This topic is locked
[SOLVED]

 Generate PDF After add event

1/7/2012 5:33:11 PM
PHPRunner General questions
lefty author

I am trying to generate a pdf on the fly using fpdf.org library. I can test the code within the root of my website; but when using in an after add event I get error ; " unable to open stream file too large error " . It errors on the line which calls the file path . Checked file paths are correct . This is running on php 5 on IIS server. No CHMOD777 . The following example is from a locked topic another member from a past post and is similiar to what I need to do.
"Quote from another member"
It's very simple ! That what Im doing right now : I have a table that contains coupons : when I click on save the new entry it shows a pdf file with all the datas I have entered ( + some editing, formatting ). I choose to display the file but you can send it right away.

All you have to do is to download FPDF from FPDF.org (it's free and use only 1 pdf file). If you want also a premade (template) pdf file, as the background use FPDI (it's also free). here is a simple code I put on the after record events:
define('FPDF_FONTPATH','/var/www/vhosts/yourlinktoyourwebhost/httpdocs/fpdf/font/');

require_once('/var/www/vhosts/yourlinktoyourwebhost/httpdocs/fpdf/fpdf.php');

require_once('/var/www/vhosts/yourlinktoyourwebhost/httpdocs/fpdf/fpdi.php');
// initiate FPDI : i use a portrait a4 page

$pdf =& new FPDI('P','mm','A4');

// add a page

$pdf->AddPage();

// set the sourcefile the file reduction.pdf is a template : it's like an empty form made with word

$pdf->setSourceFile('/var/www/vhosts/yourlinktoyourpdftemplate/fpdf/reduction.pdf');

// import page 1

$tplIdx = $pdf->importPage(1);

// use the imported page and place it at point 1,1 and 210mm large ( a4 format)

$pdf->useTemplate($tplIdx, 1, 1, 210);
// this is because I print date on the document

$today = date("d/m/Y");
// now write some text above the imported page

$pdf->SetFont('Arial','B','8');

// the setxy is where you want to write the text here it's 51mm from right margin and 65mm from top

$pdf->SetXY(51, 65);

//here I print the date

$pdf->Write(0, $today);
//mise en forme // these are because I use french date dd/mm/YYYY not YYYY-mm-dd like phprunner does
$point="***";

$montantpdf=$point."".$values["montant"]."".$point;
$date1=$values["dateemission"];
$annee=substr($date1, -10, 4);

$mois=substr($date1, -5, 2);

$jour=$date1[strlen($date1)-2].$date1[strlen($date1)-1];
$dateemis=$jour . "/" . $mois . "/" . $annee;
$date2=$values["dateexpire"];
$annee2=substr($date2, -10, 4);

$mois2=substr($date2, -5, 2);

$jour2=$date2[strlen($date2)-2].$date2[strlen($date2)-1];
$dateexp=$jour2 . "/" . $mois2 . "/" . $annee2;
//message corps texte // This is a message to write on the letter
$pdf->text(26,72,"some text for customer");
// Those thing below are specfic to my template but it's simple to adapte

// whatever you want to write 1st numer is in mm from left, second is in mm from top, then you can print text or a variable ,

//in my example I use values from fields or formatted values from fields.

//bordereau client
$pdf->text(27,187,"nom");

$pdf->text(27,197,$values["numreduc"]);

$pdf->text(25,206,$dateemis);

$pdf->text(28,215,$dateexp);

$pdf->text(33,223,$montantpdf);
//ticket client :
$pdf->text(84,186,"nom");

$pdf->text(102,194,$values["numreduc"]);

$pdf->text(84,203,$dateemis);

$pdf->text(124,203,$dateexp);

$pdf->text(93,211,$montantpdf);
// bon interne
$pdf->text(89,249,"nom");

$pdf->text(89,259,$values["numreduc"]);

$pdf->text(86,268,$dateemis);

$pdf->text(135,268,$montantpdf);

$pdf->text(93,276,$dateexp);
// Here is the magic : the name is the name you want the file to have ( whatever ) the" I " means the result page is shown immediatly in t

//the browser with embedded pdf reader, you can put a "D" instead so the brower ask to save the file , or you can save the file on

//server with "F" (you muist specify full path before the file name and have chmod777 the directory).

// Now to email the result pdf file you can use phpmailer ( free) or use built in mail() command

//$mail = new PHPMailer();

//...

//$doc = $pdf->Output('', 'S');

//$mail->AddStringAttachment($doc, 'doc.pdf', 'base64', 'application/pdf');

//$mail->Send();

// the whole pdf is transfered as a big string, and phpmailer convert it (mime decoder) and attach it to the mail.
$pdf->Output('coupon.pdf', 'I');
""End Quote"

Any Ideas here ?

C
cgphp 1/7/2012

Try to output a simple pdf like the one in the minimal example from the FPDF tutorial (http://www.fpdf.org/?lang=en).

lefty author 1/7/2012



Try to output a simple pdf like the one in the minimal example from the FPDF tutorial (http://www.fpdf.org/?lang=en).


Actually ran the code outside PHPRunner and it worked meaning the simple "Hello World" example then ran the "Hello World" example inside PHP runner after add event and I get the above error . LOL . Thanks for the input as I tried that . Any Ideas . Thanks for your input.

C
cgphp 1/7/2012

Move fpdf inside the include folder and use relative path:

define('FPDF_FONTPATH','include/fpdf/font/');

require_once('include/fpdf/fpdf.php');

require_once('include/fpdf/fpdi.php');
lefty author 1/7/2012



Move fpdf inside the include folder and use relative path:

define('FPDF_FONTPATH','include/fpdf/font/');

require_once('include/fpdf/fpdf.php');

require_once('include/fpdf/fpdi.php');




Thanks for the input : eventclass_book_display::require_once(fpd.php) [eventclass-book-display.require-once]: failed to open stream: Result too large
same problem. Wonder if it is PHP.ini issue . But if it works from the root then why? I'm stumped.

C
cgphp 1/7/2012

eventclass_book_display::require_once(fpd.php) [eventclass-book-display.require-once]: failed to open stream: Result too large



fpd.php??? It should be fpdf.php

lefty author 1/8/2012



fpd.php??? It should be fpdf.php . I now have the code above with the includes same result though.



Yes ; Saw that and renamed the folders ; was using different folders names which I suppose can be called by something I haven't seen . So changed the folders to the fpdf library names. Put the original code back in minus the mailing features. just to see if it would work or at least output to browser.

I get the same result

See Below:
Technical information

Error type 2

Error description eventclass_book_display::require_once(include/fpdf/fpdi.php) [eventclass-book-display.require-once]: failed to open stream: Result too large

Althoug the error specified above says Result too large ; I suspect something to with directory permissions . I am on windows server and using php it is a little confliction to find where to give permission and to what folder I would need to give permission . I could be barking up the wrong tree here <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=63507&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' /> but don't know where to go with this.? Thanks for any feedback . In finishing I am just trying to generate a pdf from the values of the form just added and eventually send as an email attachment. Int testing I want it to show in browser so I choose I as the variable to print to screen ?

lefty author 1/8/2012



Yes ; Saw that and renamed the folders ; was using different folders names which I suppose can be called by something I haven't seen . So changed the folders to the fpdf library names. Put the original code back in minus the mailing features. just to see if it would work or at least output to browser.

I get the same result

See Below:
Technical information

Error type 2

Error description eventclass_book_display::require_once(include/fpdf/fpdi.php) [eventclass-book-display.require-once]: failed to open stream: Result too large

Althoug the error specified above says Result too large ; I suspect something to with directory permissions . I am on windows server and using php it is a little confliction to find where to give permission and to what folder I would need to give permission . I could be barking up the wrong tree here <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=63508&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' /> but don't know where to go with this.? Thanks for any feedback . In finishing I am just trying to generate a pdf from the values of the form just added and eventually send as an email attachment. In testing I want it to show in browser so I choose I as the variable to print to screen ?

lefty author 1/8/2012






New Test with folders correct now it blows out at the fpdi call so I am getting there. See error below.
Error description require_once(fpdi_pdf_parser.php)__ [function.require-once]: failed to open stream: Result too large
Looks like theese are path problems. I will investigate the path problems and show path fix for fpdf and fpdi using phprunner in my next post . Thank you for your help . I always thought that at least in asp the paths started at the root in then down one down 2 or up 1 or up 2 ../ 1 ../ ../ 2 in php it seems it has to start at the rooot and work down.????

lefty author 1/13/2012

Okay : above problems . Including the fpdf script inside the include folder fixed the path error. Now getting error :
"Some data has already been output, can't send PDF file"
This errors when sending to browser and apparently usually has something to do with spaces in code; but since the script is in the include events file it seems it will not work here. Tried similiar script outside in the root and it runs fine. Need it to run after add event. Curious to see if anyone has been able to output a pdf from an event with phprunner .

L
laonian 1/14/2012

John,
Could you try to change this line in your #1 post:

$pdf->setSourceFile('/var/www/vhosts/yourlinktoyourpdftemplate/fpdf/reduction.pdf');


to:

$pagecount = $pdf->setSourceFile('/var/www/vhosts/yourlinktoyourpdftemplate/fpdf/reduction.pdf');


and see whether it works?
Good luck.

lefty author 1/15/2012



John,
Could you try to change this line in your #1 post:

$pdf->setSourceFile('/var/www/vhosts/yourlinktoyourpdftemplate/fpdf/reduction.pdf');


to:

$pagecount = $pdf->setSourceFile('/var/www/vhosts/yourlinktoyourpdftemplate/fpdf/reduction.pdf');


and see whether it works?
Good luck.


Thanks for the input . Using the above paths ex. var/www/vhosts don't seem to work on server I am on . Windows IIS 6.0. I believe it works for Linux. So I used relative paths . Source file was indeed the above error. Had to also put source file inside the phpinclude folder . I used another pdf template file optimized for 5.0 (seems newer pdf template don't work with library) and was able to ouput the pdf correctly to the browser . Thanks for input. Now just working on attachment of the pdf to send email code. Not sure which php email code to use. Not too familiar adding PDF files in attachments in PHP mail code.

admin 1/15/2012

There are plenty of "send PHP email with attachment" examples on the web. Check this one for example:

http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment

lefty author 2/1/2012



There are plenty of "send PHP email with attachment" examples on the web. Check this one for example:

http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment



Okay checked some of the PHP mailer libraries out there . I have tested some of them and they are all designed for multiple mime's and headers.

my problem is those scripts don't use the Output() pdf execution quite what I am looking for . Some of them returned the attachment blank. some returned the attachment with an error not readable file needs to be decoded. I am using a built in template and the database values("field") to write to the pdf and sent the attachment by email. there are some scripts here in this forum that have the functionaility to sent attachemts of a strict pdf file but not an updated on the fly pdf file and send the attachment. This is where I am stuck. Any Ideas fpdf.org scripts are either outdated or syntax problems for what I need to do. could not fiqure them out. Any Ideas. for you pdf experts out there. Remeber I am updating the fields in the pdf with values from the event page. not filling in the pdf and emailing as attachment. thanks for your help in advance.