Page 1 of 1

Generate PDF file from buffer

Posted: Mon Feb 26, 2007 11:53 am
by qqragoon
I am trying to generate PDF format from php.

PDF extention has been installed:
PDF Support: enabled
PDFlib GmbH Version: 5.0.3
PECL Version: 2.1.0
Revision: $Revision: 1.150 $

php 5.2.0 with Apache server 2.2.4.

I can get the pdf generated into a pdf file into server, and pull it out with no problem. However, i would like to get the data from buffer by using "pdf_get_buffer" function. It doesn't seem to work. Can anyone help?? Or is there another to get PDF file like pdfwriter write it directly from a html? Thanks in advanced!


Working code withouth using "pdf_get_buffer":

Code: Select all

<?php $pdf = pdf_new();
pdf_open_file($pdf,"test.pdf");
pdf_begin_page($pdf, 792,1224);

//text
$fonttwo = pdf_findfont($pdf, "Helvetica-Bold", "host", 0);
pdf_setfont($pdf, $fonttwo, 20);
pdf_show_xy($pdf, "Testing code", 30,400 );
pdf_show_xy($pdf, "Output to PDF", 30, 350);

//closeing 
pdf_end_page($pdf);
pdf_close($pdf); ?>
Non-working code with "pdf_get_buffer":

Code: Select all

<?php $pdf = pdf_new();
pdf_open_file($pdf);
pdf_begin_page($pdf, 792,1224);

//text
$fonttwo = pdf_findfont($pdf, "Helvetica-Bold", "host", 0);
pdf_setfont($pdf, $fonttwo, 20);
pdf_show_xy($pdf, "Testing code", 30,400 );
pdf_show_xy($pdf, "Output to PDF", 30, 350);

//closeing 
pdf_end_page($pdf);
pdf_close($pdf);

//get from buffer
$buffer = pdf_get_buffer($pdf);
header("Content-type: application/pdf"); 
header("Content-Length: ".strlen($buffer)); 
header("Content-Disposition: inline; filename=test.pdf"); 

echo $buffer ;
pdf_delete($pdf);  ?>

Posted: Mon Feb 26, 2007 12:07 pm
by Begby
Are you not supposed to call pdf_close() on it before trying to call the buffer function?

Posted: Mon Feb 26, 2007 12:11 pm
by qqragoon
Begby wrote:Are you not supposed to call pdf_close() on it before trying to call the buffer function?
Yes, you do close it first. Then, get from buffer.

ref: http://www.phpdig.net/ref/rn46re911.html

Posted: Mon Feb 26, 2007 1:56 pm
by pickle
I understand you want to use the simplest function: pdf_get_buffer(). But if that doesn't work, have you tried retrieving the buffer contents yourself using ob_start() & ob_get_clean()?

Actually, the user comments for pdf_get_buffer() say you have to call pdf_open_file()with an empty filename.

Posted: Thu Mar 01, 2007 1:24 pm
by qqragoon
I still couldn't get it to work after few other tries. I did give the pdf file a empty file name, and what I got is the following output. Can anyone help? Am I suppose to get a window ask me to open/save the pdf instead of this output? thanks!

Code: Select all

<?php

$pdf = pdf_new();

if (!pdf_open_file($pdf, "")) {
   echo "error";
   exit;
};

pdf_begin_page($pdf, 595, 842);

//text 
$fonttwo = pdf_findfont($pdf, "Helvetica-Bold", "host", 0); 
pdf_setfont($pdf, $fonttwo, 20); 
pdf_show_xy($pdf, "Testing code", 30,400 ); 
pdf_show_xy($pdf, "Output to PDF", 30, 350); 

//closeing 
pdf_end_page($pdf); 
pdf_close($pdf); 

//get from buffer
$buf = pdf_get_buffer($pdf);
$len = strlen($buf);

header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=foo.pdf");
print $buf; 

pdf_delete($pdf);
?>
output:

%PDF-1.4 %äãÏÒ 3 0 obj <> stream xœs áÒw3P02PIã2T0BCc H.H܇K#$µ¸$3/]!9?%U3$ Y¡±)X¡†iIAi‰BI¾B€‹H

Posted: Thu Mar 01, 2007 3:16 pm
by pickle
Change the Content-Disposition to 'attachment' instead of 'inline'.