This topic is locked

HTML to PDF on the fly

3/8/2017 11:42:58 PM
PHPRunner General questions
lefty author

**Here is My Code . Can use some help with this one. A little complicated.

I can get the pdf to open in the browser , which I don't want . I just want the formatted/Templated HTML to be converted to pdf and sent in email on the fly . I am using DOMPDF plugin for this project which is included in PHPrunner 9.6. build 27826. I just get an email with the word ARRAY and nothing else. Have Tried different combinations and none of them work. Most do not even send email. Gets stuck at Attachments.

_
After ADD EVENT_
Here is my code
require_once('C:\\Inetpub\\vhosts\\mywebsite.com\httpdocs/2017b/plugins/dompdf/dompdf_config.inc.php');

spl_autoload_register('DOMPDF_autoload');
// instance dompdf

$dompdf = new DomPDF();

$dompdf->set_paper(DEFAULT_PDF_PAPER_SIZE, 'portrait');
// tell the user-agent to expect a PDF

header('Content-type: application/pdf');
// load the HTML,

$src = file_get_contents('Support_form3b.htm');

// replace variables

$src = str_replace("##flddate##",date("m/d/Y",strtotime($values["orderdate"])),$src);

$src = str_replace("##fldname##",$values["fldname"],$src);

$src = str_replace("##EmployeeID##",$values["EmployeeID"],$src);

$src = str_replace("##fldsubject##",$values["fldsubject"],$src);

$src = str_replace("##fldmemo##",$values["fldmemo"],$src);

$src = str_replace("##fldemail##",$values["fldemail"],$src);
//convert to pdf output
$dompdf->load_html($src);

$dompdf->render();
//$dompdf->stream('mypdf.pdf');
// This works and sends output to browser right away which I don't want , It is supposed to be sent to administrator of this table.


// so I tried this

file_put_contents('C:\\Inetpub\\vhosts\\mywebsite.com\httpdocs/2017b/plugins/dompdf/mypdf.pdf');

// I think this is the line that is giving me trouble , I don't think it writes to mypdf.pdf file . I have a temporary blank template setup in the dompdf folder with write access. mypdf.pdf.
$from = "Noreply@mywebsite.com";

$to = "myadmin@mywebsite.com";

$subject="Documents";
$attachments = array();

// Attachments description. The 'path'(a path to the attachment) is required. Others parameters are optional:

//'name' overrides the attachment name, 'encoding' sets a file encoding, 'type' sets a MIME type

$attachments = array(

array('path' => getabspath('C:\\Inetpub\\vhosts\\mywebsite.com\httpdocs/2017b/plugins/dompdf/mypdf.pdf')),
// This is another line I am unsure of since it uses getaabspath.
'encoding' => 'base64',

'type' => 'application/pdf',
);
$ret = runner_mail(array('from' => $from, 'to' => $to, 'subject' => $subject, 'attachments' => $attachments));
if(!$ret["mailed"]){

echo $ret["message"];

}
And the dompdf.config.custome.inc setup. Which Can cause Issues . Dompdf on github documentation is not the best. uncommented the lines I think I should be using. The only one I am not sure about is the first line /tmp. Reason why DOMPDF_ENABLE, PHP is commented out as my reasearch tells me with define("DOMPDF_ENABLE_REMOTE", true); and php enabled it is a security issue. when I did uncomment I got a php file instead of pdf file for output but no email . not working.
define("DOMPDF_TEMP_DIR", "/tmp");

define("DOMPDF_CHROOT", DOMPDF_DIR);

define("DOMPDF_FONT_DIR", DOMPDF_DIR."/lib/fonts/");

define("DOMPDF_FONT_CACHE", DOMPDF_DIR."/lib/fonts/");

define("DOMPDF_UNICODE_ENABLED", true);

//define("DOMPDF_PDF_BACKEND", "PDFLib");

define("DOMPDF_DEFAULT_MEDIA_TYPE", "print");

define("DOMPDF_DEFAULT_PAPER_SIZE", "letter");

define("DOMPDF_DEFAULT_FONT", "arial");

//define("DOMPDF_DPI", 72);

//define("DOMPDF_ENABLE_PHP", true);

define("DOMPDF_ENABLE_REMOTE", true);

define("DOMPDF_ENABLE_CSS_FLOAT", true);

//define("DOMPDF_ENABLE_JAVASCRIPT", false);

//define("DEBUGPNG", true);

//define("DEBUGKEEPTEMP", true);

//define("DEBUGCSS", true);

//define("DEBUG_LAYOUT", true);

//define("DEBUG_LAYOUT_LINES", false);

//define("DEBUG_LAYOUT_BLOCKS", false);

//define("DEBUG_LAYOUT_INLINE", false);

//define("DOMPDF_FONT_HEIGHT_RATIO", 1.0);

//define("DEBUG_LAYOUT_PADDINGBOX", false);

//define("DOMPDF_LOG_OUTPUT_FILE", DOMPDF_FONT_DIR."log.htm");

//define("DOMPDF_ENABLE_HTML5PARSER", true);

define("DOMPDF_ENABLE_FONTSUBSETTING", true);
// DOMPDF authentication

//define("DOMPDF_ADMIN_USERNAME", "user");

//define("DOMPDF_ADMIN_PASSWORD", "password");
I have given write/read permissions to dompdf / templatesc / dompdf / fonts and font-library.

The email just sends a message with the word ARRAY . no attachment.

Want to attach the Templated email that was converted in html to pdf.

I have changed custom.config in dompdf to what I think is the correct settings like define( Remote, True ) etc.... to get images. So it converts somewhat okay except for hyperlinks which I will deal with later.

I have a temporary blank template setup in the dompdf folder with write access. mypdf.pdf.

Anyone have any ideas on this . There are custom mailers for php but I use built in runnermail function for other tables in same project , so I cannot change the attachments array and use php mail or PHPMAILER for this function As far as I know.**
I would even work with a solution that send the pdf view on print / view page to recipient / although it is not formatted the way I want it like my html form.

HJB 3/10/2017

https://xlinesoft.com/livedemo/invoice/livedemo1/invoices_add.php
John, the INVOICE template is exactly doing of what you are looking for.

Say, you just click on "Send Invoice" button on top and a window

is popping up where you can make adjustments/modifications on subject,

mail text (while the email address is pulled from the client table) and

if all is okay, you finally click on SEND.
So, a $49 investment would not solve your problem, but enable you to show

up with that very functionality among your clients which induces them to

immediately fall in love with sophisticated apps, forever;-)

lefty author 3/10/2017



https://xlinesoft.com/livedemo/invoice/livedemo1/invoices_add.php
John, the INVOICE template is exactly doing of what you are looking for.

Say, you just click on "Send Invoice" button on top and a window

is popping up where you can make adjustments/modifications on subject,

mail text (while the email address is pulled from the client table) and

if all is okay, you finally click on SEND.
So, a $49 investment would not solve your problem, but enable you to show

up with that very functionality among your clients which induces them to

immediately fall in love with sophisticated apps, forever;-)



Yea saw that . I might have to buy it , just to see it's functionality but it just creates the invoice, Which I can do . If you copy the link on the demo page to view it does not come up . Not sure the demo is working right anyway, Plus my tables have advanced security ( I guess that's what the hash is for ) . I just can't get the pdf output to attach to the email . Output works if I use stream. and opens browser or download. I guess $49 is worth it to see how it's setup . I just need to send it behind the scene to master admin of that table. I don't want the user to see any pdf output. Just submit and done.Then after add it goes by pdf as an attachment. LIke I have a popup script now ( your form has been submitted . Success. after add.

I sent the demo to my email address and it just has a link which just loads and never outputs the invoice. so it is not working anyway. If that don't work it sure is not going to work for me unless I find it's error.

Thanks.

HJB 3/10/2017

but it just creates the invoice

Unquote

It is NOT, I bought the template long time ago

and used it, so, when I tell you, after SEND INVOICE

a windows pops up with all in = e-mail address, subject

and pre-defined body text to review/modify at will, the

final click on the seen SEND button = you're fully done!

lefty author 3/10/2017

[size="3"][/size]Will Check it out . Thanks
Looking at the demo of Invoice template does not help my cause . If I could have seen the source code it might but the user cannot be the one to type the emails and recipient should not have to hit a link to see the pdf file invoice. So that is not where I am headed. I have tested the code below and this is what I have so far.
require_once('C:\\Inetpub\\vhosts\\mywebsite.com\httpdocs/2017b/plugins/dompdf/dompdf_config.inc.php');

spl_autoload_register('DOMPDF_autoload');
// instance dompdf

$dompdf = new DomPDF();

$dompdf->set_paper(DEFAULT_PDF_PAPER_SIZE, 'portrait');
// load the HTML,

$src = file_get_contents('Support_form3b.htm');

// replace variables

$src = str_replace("##flddate##",date("m/d/Y",strtotime($values["flddate"])),$src);

$src = str_replace("##fldname##",$values["fldname"],$src);

$src = str_replace("##EmployeeID##",$values["EmployeeID"],$src);

$src = str_replace("##fldsubject##",$values["fldsubject"],$src);

$src = str_replace("##fldmemo##",$values["fldmemo"],$src);

$src = str_replace("##fldemail##",$values["fldemail"],$src); // this loads my template which works pretty good - need some adjustments but works.
//convert to pdf output

$dompdf->load_html($src);

$dompdf->render();

$output = $dompdf->output();

file_put_contents('C:\\Inetpub\\vhosts\\mywebsite.com\httpdocs/2017b/plugins/dompdf/mypdf.pdf', $output); // This writes the file in the directory and works fine.
// Send Email with attachment - Email will send witouht array for attachment .

$from = "test@test.com";
$to = "test2@test2.com";
$msg = "Find some documents (Invoice.pdf, Photo.jpg, signature.jpg) attached.";
$subject="Documents";
$attachments = array();
// Attachments description. The 'path'(a path to the attachment) is required. Others parameters are optional:
//'name' overrides the attachment name, 'encoding' sets a file encoding, 'type' sets a MIME type
$attachments = array(
array('path' => getabspath('../plugins/dompdf/mypdf.pdf')), // This is where the problem is . It either won't get the file or decoding / encoding is wrong so it stops here with no error warnings in devloper mode.
'encoding' => 'base64', [size="3"]// should this be UTF - 8 ? or should there be some econding/decoding of the file done first.[/size]
'type' => 'application/octet-stream',
) ;
$ret = runnermail(array('from' => $from, 'to' => $to, 'subject' => $subject, 'body' => $msg, 'attachments' => $attachments));
if(!$ret["mailed"]){
echo $ret["message"];
}
[size="3"]
The PDF gets generated and is overwritten in the folder I have it available and the overwrite is fine with me ( except that I actually can have a collision if someone submits the form at the same time) doubtfull though only 150 users with 20 different forms. So If I fill out a form the pdf generates and writes to the directory I want and with the name I want mypdf.pdf. Works fine.
Now the hard part . Emailing the PDF is an issue. I am trying to do this without using phpmailer as I have tried all variations and I keep getting pdf mail could not be opened ( pdf file because it is corrupted or has not been decoded.) . ( by checking use PHP mailer ( not recommended ) in the newer versions )
I thought if I use Runner_mail wrapper and try to get the file from the directory like you would do in an upload which works I can grab the attachment.

_[/size] ex... File_Get_Contents(....
My take on this is , If you can use runner_mail wrapper to add an attachment of a file that was uploaded , why would I not be able to go and get that same file and attach it. Now I know it must have something to do with upload function and Json . I have some ideas of the code but not sure where to put it and how to . obviously.!!!
Thanks for any replies.
Any Ideas would help not just me but some others here that have tried this in the past going back to fpdf.
In my opinion since being a member 2005 . This option should have been added a long time ago as an easy addon or part of phprunner. Can't be added to asprunner as their library is out of date . but asp.net would work also. Why is there a wait time for this option as it's been asked in the forum for 12 years. I could not care less if you added extra $$$$ to the upgrade just do it. It would make everyone's coding a lot more simpler.

lefty author 3/17/2017

I have looked at phpmailer examples here . Just don't work with attachments. especially pdf files.

HJB 3/18/2017

In my opinion since being a member 2005 . This option should have been added a long time ago as an easy addon or part of phprunner. Can't be added to asprunner as their library is out of date . but asp.net would work also. Why is there a wait time for this option as it's been asked in the forum for 12 years. I could not care less if you added extra $$$$ to the upgrade just do it. It would make everyone's coding a lot more simpler.

[/quote]
A since 2002 runner (PHPRunner v1.0) like had been writing to you to that exactly what you are looking for is in full functionality by default when ordering the INVOICE template. Reason to go this way is simple. To develop and produce a software which is direct to a majority of user demands is one thing and not an easy task at all. Once such developers are rendering templates at a price, it does NOT mean that you get the 100% perfect wished solution into your hands, but basic functionality from which one is the able to make changes at will. I just installed a demo site and can upload the INVOICE template to show to you that indeed one has NOT been lying at all, but had just commenting by means of a buying experience on a template. Next to it, after online inspection by you, I can save the project as a TEMPLATE to be sold to you at a symbolic since 200ß5 forum member price of $1 (one buck). Though I myself had been seeing that it took it several years after my proposals made on this forum to e.g. implement code snippets into dashboards or the PHP desktop stand alone issue and lots more things, I must say: Over time, they realized it to implement things and at least me is NOT seeing any reason at all to make NOISE, simply because the forum rules could be read on top, saying, this is NOT a support forum at all, if you need support, you can buy same at a price, at least for me, very fair rates as of $85/h as per https://xlinesoft.com/buy_support.htm page content.
Finally, I'm definitely NO friend for loud and hefty "Open Source for all, please, free of charge" calls in public at all as indeed any company has monthly expenses like salaries, profit, rent and tons more to cover.
PHPRunner v9.7 is the most flexible RAD tool on this planet for which at lest me has to truly say MANY THANKS, including shown support since PHPR v1.0, in short "Hail the developers!", and nothing else, please.

C
chrispa 3/19/2017

Great topic exactly what i need also to implement.

have try your code and have not yet manage to implement and create succesfully the pdf file and that because my htl contains master/detail data.

when it comes to detail part it looks that it takes long time or never completes my code below
quote
$sql = "SELECT * FROM tblrequisitionsuppliesdetails WHERE EnquryLink=".$keys["IdCounter"]."";
$rs = CustomQuery($sql);
$src =file_get_contents('rfq.html');
while ($data = db_fetch_array($rs)){
// replace variables

$itemcode.='<div>'.$data[ShortCode].'</div>';

echo $itemcode;

echo $itemName;
$itemName.='<div>'.$data["Description"].'</div>';
$itemquantity.='<div>'.$data["Quantity"].'</div>';
$itemunit.='<div>'.$data["UnitLink"].'</div>';
//$itemCode.='<div>'.$data[ShortCode].'</div>';
}
$src= str_replace("##ItemCode##",$itemcode,$src);
$src= str_replace("##ItemName##",$itemName,$src);

$src= str_replace("##ItemQty##",$itemquantity,$src);

$src= str_replace("##ItemPcs##",$itemunit,$src);
require_once('/plugins/dompdf/dompdf_config.inc.php');

spl_autoload_register('DOMPDF_autoload');
// instance dompdf

$dompdf = new DomPDF();

$dompdf->set_paper(DEFAULT_PDF_PAPER_SIZE, 'portrait');

header('Content-type: application/pdf');
//$itemCode.='<div>'.$data[ShortCode].'</div>';
$src =str_replace("##VesselName##",$values['VesselLink'],$src);

$src = str_replace("##VesselRefNo##",$values['OurReferNo'],$src);

$src = str_replace("##Date##",date("d.m.y"),$src);
//convert to pdf output

$dompdf->load_html($src);

$dompdf->render();

$output = $dompdf->output();

file_put_contents('c:/mypdf.pdf', $output);
unquote

--------
any suggestions will be appreciated .
i will start working on email attchment as soon as i have complete succesfully the export to pdf part.
thanks

C
chrispa 3/19/2017

Great topic exactly what i need also to implement.

have try your code and have not yet manage to implement and create succesfully the pdf file and that because my html contains master/detail data.

when it comes to detail part it looks that it takes long time or never completes my code below
quote
$sql = "SELECT * FROM tblrequisitionsuppliesdetails WHERE EnquryLink=".$keys["IdCounter"]."";
$rs = CustomQuery($sql);
$src =file_get_contents('rfq.html');
while ($data = db_fetch_array($rs)){
// replace variables

$itemcode.='<div>'.$data[ShortCode].'</div>';

echo $itemcode;

echo $itemName;
$itemName.='<div>'.$data["Description"].'</div>';
$itemquantity.='<div>'.$data["Quantity"].'</div>';
$itemunit.='<div>'.$data["UnitLink"].'</div>';
//$itemCode.='<div>'.$data[ShortCode].'</div>';
}
$src= str_replace("##ItemCode##",$itemcode,$src);
$src= str_replace("##ItemName##",$itemName,$src);

$src= str_replace("##ItemQty##",$itemquantity,$src);

$src= str_replace("##ItemPcs##",$itemunit,$src);
require_once('/plugins/dompdf/dompdf_config.inc.php');

spl_autoload_register('DOMPDF_autoload');
// instance dompdf

$dompdf = new DomPDF();

$dompdf->set_paper(DEFAULT_PDF_PAPER_SIZE, 'portrait');

header('Content-type: application/pdf');
//$itemCode.='<div>'.$data[ShortCode].'</div>';
$src =str_replace("##VesselName##",$values['VesselLink'],$src);

$src = str_replace("##VesselRefNo##",$values['OurReferNo'],$src);

$src = str_replace("##Date##",date("d.m.y"),$src);
//convert to pdf output

$dompdf->load_html($src);

$dompdf->render();

$output = $dompdf->output();

file_put_contents('c:/mypdf.pdf', $output);
unquote

--------
any suggestions will be appreciated .
i will start working on email attachment as soon as i have complete succesfully the export to pdf part.
thanks

C
chrispa 3/20/2017

hello
quote
global $dal;
require_once('/plugins/dompdf/dompdf_config.inc.php');

spl_autoload_register('DOMPDF_autoload');

$dompdf = new DomPDF();

$dompdf->set_paper(DEFAULT_PDF_PAPER_SIZE, 'portrait');
$src = file_get_contents('order.txt');
$sql1 = "SELECT * FROM tblrequisitionsuppliesdetails WHERE EnquryLink=".$keys["IdCounter"]." ";

$rs1 = CustomQuery($sql1);

$data1 = db_fetch_array($rs1);
while($data1 = db_fetch_array($rs1))

{
// Date formats
$code ='<div>'.$data1['ShortCode'].'</div>';

$code1 ='<div>'.$data1['Description'].'</div>';
$src= str_replace("##ItemCode##",$code,$src);
$src= str_replace("##ItemName##",$code1,$src);
}
$src =str_replace("##VesselName##",$values['VesselLink'],$src);

$src = str_replace("##VesselRefNo##",$values['OurReferNo'],$src);

$src = str_replace("##Date##",date("d.m.y"),$src);
$dompdf->load_html($src);

$dompdf->render();

$output = $dompdf->output();
file_put_contents('c:/mypdf.pdf', $output);
unquote
can you help me child records
$code ='<div>'.$data1['ShortCode'].'</div>';

$code1 ='<div>'.$data1['Description'].'</div>';
returns only 1 value to PDF and not multiple have check with echo and returns values but not in pdf ???
thanks in advance

lefty author 3/23/2017



In my opinion since being a member 2005 . This option should have been added a long time ago as an easy addon or part of phprunner. Can't be added to asprunner as their library is out of date . but asp.net would work also. Why is there a wait time for this option as it's been asked in the forum for 12 years. I could not care less if you added extra $$$$ to the upgrade just do it. It would make everyone's coding a lot more simpler.


Walkfly:

Do you use invoice template? Can you tell me if the link that you get in the email works as in the demo . I can't even load the pdf file on the invoice page in demo . Tried on two different computers and OS and three browsers. Then in the link in email from the demo. I click the link and it just sits there before timing out. Before I purchase Just wanted to know if you have this working . Also Just read another post . How do you incoporate into project I guess I can just see the code and copy into my tables is that what you are refering to? As far as support , I have no problem paying for support . Notice the amount of views on this subject. But I must note . When I practice golf I like to work on different things. If it is a real serious issue with my swing then I see a Pro for lessons.But I always try to fix my game myself first. As far as being the best RAD tool on the planet . I definitely agree as working full time in another field this gives me nights and weekends to develop . Without it I probably would have gotten out of my part time developing as there really is nothing else like it and all other API's or editors just , well take too long. But PDF building has taken me a couple of years and I still can't seem to get it the way I want( used to try in asp but there was only one library left and it was in Italian . Even though I'm Italian I still could not get it right (LOL) . So It's probably time to get the pro to help (support) . ( used to be asp classic guy so still learning PHP - in progress. I once wrote a project for a client in 90 minutes with 4 tables and email events. So I agree totally . Thanks for the info.

C
chrispa 3/25/2017

Hi JOHN,
Following code worked fine me as far as related with sending pdf as email attachment after you generate pdf . My problem remains the child records <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=81780&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />
quote
$to = "abc@gmail.com";

$subject = "mail with attachment";
$att = file_get_contents( 'generated.pdf' );

$att = base64_encode( $att );

$att = chunk_split( $att );
$BOUNDARY="anystring";
$headers =<<<END

From: Your Name <abc@gmail.com>

Content-Type: multipart/mixed; boundary=$BOUNDARY

END;
$body =<<<END

--$BOUNDARY

Content-Type: text/plain
See attached file!
--$BOUNDARY

Content-Type: application/pdf

Content-Transfer-Encoding: base64

Content-Disposition: attachment; filename="your-file.pdf"
$att

--$BOUNDARY--

END;
mail( $to, $subject, $body, $headers );

lefty author 3/25/2017



Hi JOHN,
Following code worked fine me as far as related with sending pdf as email attachment after you generate pdf . My problem remains the child records <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=81783&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />
quote
$to = "abc@gmail.com";

$subject = "mail with attachment";
$att = file_get_contents( 'generated.pdf' );

$att = base64_encode( $att );

$att = chunk_split( $att );
$BOUNDARY="anystring";
$headers =<<<END

From: Your Name <abc@gmail.com>

Content-Type: multipart/mixed; boundary=$BOUNDARY

END;
$body =<<<END

--$BOUNDARY

Content-Type: text/plain
See attached file!
--$BOUNDARY

Content-Type: application/pdf

Content-Transfer-Encoding: base64

Content-Disposition: attachment; filename="your-file.pdf"
$att

--$BOUNDARY--

END;
mail( $to, $subject, $body, $headers );


Tried above code . No email sent.

I believe there is a problem with this line
$att // this is grayed out in editor (intellisense) I have mail function checked also. Probably will get some support as I've already spent too much time on this one.
--$BOUNDARY--

END;