liltyga
asked on
Urgent: Dynamic PDF file creation and saving with PHP using pdf-php
Hi, I have sample code working using the pdf-php class (documentation on that is here: http://www.ros.co.nz/pdf/ , and in the documentation it says that saving to disk with the code is trivial, but I can't seem to get it to work with the examples. I have the pdf generating the way I want, but I need to have the file saved as a unique file name upon creation. Can anyone please help?
> I have the pdf generating the way I want, but I need to have the file saved as a unique file name upon creation. Can anyone please help?
Once the $pdf is complete, you can save it to a file like this:
---
$handle = fopen('c:\\setup\\test2.pd f', 'w+');
fwrite($handle, $pdf->ezOutput(1));
fclose($handle);
---
Once the $pdf is complete, you can save it to a file like this:
---
$handle = fopen('c:\\setup\\test2.pd
fwrite($handle, $pdf->ezOutput(1));
fclose($handle);
---
ASKER
Hi,
Thanks for your help. I'm not sure what I'm doing wrong, but I'm still having issues with this. Here is the code I have so far; maybe that will be helpful - I didn't add the open and close, so that maybe you can show me exactly where to put it. Sorry for being such a dunce on this!
<?php
//======================== ========== ========== ========== ========== ========== ========== ========== =====
// this is the php file which creates the final printing output file
//======================== ========== ========== ========== ========== ========== ========== ========== =====
// don't want any warnings turning up in the pdf code if the server is set to 'anal' mode.
//error_reporting(7);
error_reporting(E_ALL);
set_time_limit(1800);
include 'class.ezpdf.php';
// define a clas extension to allow the use of a callback to get the table of contents, and to put the dots in the toc
class Creport extends Cezpdf {
var $reportContents = array();
function Creport($p,$o){
$this->Cezpdf($p,$o);
}
function rf($info){
// this callback records all of the table of contents entries, it also places a destination marker there
// so that it can be linked too
$tmp = $info['p'];
$lvl = $tmp[0];
$lbl = rawurldecode(substr($tmp,1 ));
$num=$this->ezWhatPageNumb er($this-> ezGetCurre ntPageNumb er());
$this->reportContents[] = array($lbl,$num,$lvl );
$this->addDestination('toc '.(count($ this->repo rtContents )-1),'FitH ',$info['y ']+$info[' height']);
}
function dots($info){
// draw a dotted line over to the right and put on a page number
$tmp = $info['p'];
$lvl = $tmp[0];
$lbl = substr($tmp,1);
$xpos = 520;
switch($lvl){
case '1':
$size=16;
$thick=1;
break;
case '2':
$size=12;
$thick=0.5;
break;
}
$this->saveState();
$this->setLineStyle($thick ,'round',' ',array(0, 10));
$this->line($xpos,$info['y '],$info[' x']+5,$inf o['y']);
$this->restoreState();
$this->addText($xpos+5,$in fo['y'],$s ize,$lbl);
}
}
// Make a new pdf:
$pdf = new Cezpdf('a4','landscape');
// Select a font to use:
$pdf->selectFont('./fonts/ Helvetica. afm');
// Insert jpeg image in upper left and make it 600 points wide.
//add first row of images
$pdf->addJpegFromFile("$fi le", 1, 445, 252, 144);
$pdf->addJpegFromFile("$fi le", 1, 300, 252, 144);
$pdf->addJpegFromFile("$fi le", 1, 155, 252, 144);
$pdf->addJpegFromFile("$fi le", 1, 10, 252, 144);
//add second row of images
$pdf->addJpegFromFile("$fi le", 253, 445, 252, 144);
$pdf->addJpegFromFile("$fi le", 253, 300, 252, 144);
$pdf->addJpegFromFile("$fi le", 253, 155, 252, 144);
$pdf->addJpegFromFile("$fi le", 253, 10, 252, 144);
//add third row of images
$pdf->addJpegFromFile("$fi le", 506, 445, 252, 144);
$pdf->addJpegFromFile("$fi le", 506, 300, 252, 144);
$pdf->addJpegFromFile("$fi le", 506, 155, 252, 144);
$pdf->addJpegFromFile("$fi le", 506, 10, 252, 144);
// Send to browser
$pdf->ezStream();
?>
Thanks for your help. I'm not sure what I'm doing wrong, but I'm still having issues with this. Here is the code I have so far; maybe that will be helpful - I didn't add the open and close, so that maybe you can show me exactly where to put it. Sorry for being such a dunce on this!
<?php
//========================
// this is the php file which creates the final printing output file
//========================
// don't want any warnings turning up in the pdf code if the server is set to 'anal' mode.
//error_reporting(7);
error_reporting(E_ALL);
set_time_limit(1800);
include 'class.ezpdf.php';
// define a clas extension to allow the use of a callback to get the table of contents, and to put the dots in the toc
class Creport extends Cezpdf {
var $reportContents = array();
function Creport($p,$o){
$this->Cezpdf($p,$o);
}
function rf($info){
// this callback records all of the table of contents entries, it also places a destination marker there
// so that it can be linked too
$tmp = $info['p'];
$lvl = $tmp[0];
$lbl = rawurldecode(substr($tmp,1
$num=$this->ezWhatPageNumb
$this->reportContents[] = array($lbl,$num,$lvl );
$this->addDestination('toc
}
function dots($info){
// draw a dotted line over to the right and put on a page number
$tmp = $info['p'];
$lvl = $tmp[0];
$lbl = substr($tmp,1);
$xpos = 520;
switch($lvl){
case '1':
$size=16;
$thick=1;
break;
case '2':
$size=12;
$thick=0.5;
break;
}
$this->saveState();
$this->setLineStyle($thick
$this->line($xpos,$info['y
$this->restoreState();
$this->addText($xpos+5,$in
}
}
// Make a new pdf:
$pdf = new Cezpdf('a4','landscape');
// Select a font to use:
$pdf->selectFont('./fonts/
// Insert jpeg image in upper left and make it 600 points wide.
//add first row of images
$pdf->addJpegFromFile("$fi
$pdf->addJpegFromFile("$fi
$pdf->addJpegFromFile("$fi
$pdf->addJpegFromFile("$fi
//add second row of images
$pdf->addJpegFromFile("$fi
$pdf->addJpegFromFile("$fi
$pdf->addJpegFromFile("$fi
$pdf->addJpegFromFile("$fi
//add third row of images
$pdf->addJpegFromFile("$fi
$pdf->addJpegFromFile("$fi
$pdf->addJpegFromFile("$fi
$pdf->addJpegFromFile("$fi
// Send to browser
$pdf->ezStream();
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks leannonn,
Sorry, I should have made myself more clear. Ideally, option 2 would be the best. When I replace
---
$pdf->ezStream();
---
with:
---
$my_options = array("Content-Disposition " => "my_file_name.pdf");
$pdf->ezStream($my_options );
---
the document renders, but I don't see it saved anywhere. I'm not sure what's happening with that.
For option 1, if I can have that working, that would be great, but I need to change the path from fopen('c:\\setup\\test2.pd f', 'w+'); to a relative path structure, like /usr/home/custom/htdocs/pd f-php/ - but I get an error when I try to do that. Could you let me know how I can reference the web root without getting the error? thanks again for your help
Sorry, I should have made myself more clear. Ideally, option 2 would be the best. When I replace
---
$pdf->ezStream();
---
with:
---
$my_options = array("Content-Disposition
$pdf->ezStream($my_options
---
the document renders, but I don't see it saved anywhere. I'm not sure what's happening with that.
For option 1, if I can have that working, that would be great, but I need to change the path from fopen('c:\\setup\\test2.pd
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sweet! Thanks so much - I used ./your_file.pdf and it worked! You're the best :)
Cant you do it like this:
First create the pdf
then put the pdf in a file with a uniqe name with the commands.
$tmpfname = tempnam("/tmp", "PDF");
$handle = fopen($tmpfname, "w");
fwrite($handle, $pdf_code);
fclose($handle);
Here is the code to output a pdf on the page:
$len = filesize($filename);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Dispositio
readfile($filename);
/Rob