pdflib : Show on browser and save on server in the same time a pdf file

2.3k views Asked by At

Is somebody know how to show on browser and save on server in the same time a pdf file? I use PHP and PDFLIB to generate the pdf, and in the beginning i do :

PDF_open_file ($p, $DocFile);

Where $DocFile indicate the place on server where i write the pdf. And at the end , when i finished generating the pdf, in order to save the pdf somewhere on the server ,i do :

$b = PDF_close ($p); $b = PDF_delete ($p);

I can save the file on the server but what i want to do now is to be able to save the pdf on the server and the same time display it on the browser. How can i do that? Im using Pdflib v7 with php v5.

2

There are 2 answers

1
Rina Certis  or AHR On BEST ANSWER

I found myself the solution, i put it here if somebody need:

<? PDF_open_file ($p, "/tmp/test.pdf"); $pdf = pdf_new(); pdf_open_file($pdf, "/tmp/test.pdf"); pdf_set_info($pdf, "Author", "Horvath Tamas"); pdf_set_info($pdf, "Title", "Szamla"); pdf_set_info($pdf, "Creator", "ARBOMedia.net Kft"); pdf_set_info($pdf, "Subject", "Szamla"); pdf_begin_page($pdf, 595, 842); pdf_add_outline($pdf, "Page 1"); pdf_set_font($pdf, "Times-Roman", 30, "host"); pdf_set_value($pdf, "textrendering", 1); pdf_show_xy($pdf, "Hello world", 50, 750); pdf_moveto($pdf, 50, 740); pdf_lineto($pdf, 330, 740); pdf_stroke($pdf); pdf_end_page($pdf); pdf_close($pdf); pdf_delete($pdf); $len = filesize("/tmp/test.pdf"); header("Content-type: application/pdf"); header("Content-Length: $len"); header("Content-Disposition: inline; filename=test.pdf"); readfile("/tmp/test.pdf"); ?>

0
Rainer On

The solution Rina Certi offer is fine in general, (except some wrong usage of some APIs), but you can also do it the other way around:

  • create the PDF in memory
  • retrieve the PDF file via PDF_get_buffer() from memory, and send this to the client and save this data to disc, by using the PHP fwrite() or file_put_contents().

BTW, PDF_open_file(), PDF_close() are deprecated since PDFlib 6. Please use PDF_begin_document() and PDF_end_document() instead. To load and set a font, please use