Generate PDF file from buffer

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
qqragoon
Forum Newbie
Posts: 17
Joined: Wed Nov 15, 2006 6:17 pm

Generate PDF file from buffer

Post 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);  ?>
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Are you not supposed to call pdf_close() on it before trying to call the buffer function?
qqragoon
Forum Newbie
Posts: 17
Joined: Wed Nov 15, 2006 6:17 pm

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
qqragoon
Forum Newbie
Posts: 17
Joined: Wed Nov 15, 2006 6:17 pm

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Change the Content-Disposition to 'attachment' instead of 'inline'.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply