954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Generate PDF and then attach it with PHP?

Hi. I’m creating a system for my client where he goes to a page where he types in a letter. The text is saved as a file (preferably PDF) and then sent as an email to a printing service. I’d prefer not to save multiple PDFs on my server, so I’ve tried saving the text to a database and then processing a GET function to display the letter as a PDF (e.g. ‘/pdf.php?id=3’ would pull up the text with the id ‘3’). My problem is then to attach it. Also, is a there a way to convert HTML/CSS to PDF with PHP?
Any suggestions?

Thanks.

calebcook
Junior Poster in Training
66 posts since Jun 2011
Reputation Points: 10
Solved Threads: 4
 

Back in the day (dos and cp/m) you could send a file direct to the printer by "saving" it to :prn - so if the problem is that php wants always to save a created pdf as a file, then possibly you could redirect the output to the screen in a similar way. This is just an idea, of course. Maybe somebody else could come up with the "filename" for sending a file to the screen, if this is still possible.

Alternatively, you could save it as a file, and do a regular sweep to delete them so as not to clutter up the server.

There's a PECL extension adding pdf stuff to php. All the requirements and so on can be found here: http://php.net/manual/en/book.pdf.php

tiggsy
Junior Poster
119 posts since Jul 2009
Reputation Points: 16
Solved Threads: 8
 

You might want to look at Qoppa Software's commercial products, as we have a variety of options to get your input into PDF that can be emailed: Server: PDF Automation Server watches a port or input location, processes the input per pre-defined rules (such as stamping, markup, etc.), then outputs to pre-defined location(s), including email.
Mobile: Our qPDF Notes (Android App) allows you to send PDF files via email right from your Android device!
Desktop: PDF Studio will let you open files of a variety of types (even whole directories at a time), assemble, disassemble, markup, comment, and more, then save the resulting PDF to whatever location you'd like. You still need to have a separate step to get it into email.

ValueGal
Newbie Poster
1 post since Sep 2010
Reputation Points: 8
Solved Threads: 0
 

You can create the pdf file using your php code (or if you don't have, use: http://php.net/pdf ), then, if you need to print, use the ability to download the pdf file content just created. If user have pdf reader installed, he can use the print button from this application. The tutorial is here: http://www.php.net/pdf.examples-basic.php

<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// to show the file size on browser download window, and calculate the download time
header('Content-Length: '.filesize('original.pdf'));

// The PDF source is in original.pdf
readfile('original.pdf');
?>


Note: you can display the download dialogue in browser usingattachment (like in my example), or if you want to display in page use inline.

eXpertPHP
Newbie Poster
20 posts since Aug 2011
Reputation Points: 10
Solved Threads: 7
 

You can convert HTML to PDF quite easily with HTMLDoc ( http://www.htmldoc.org/ ) but it doesn't handle CSS very well, as I recall. I have used it quite successfully but I did have to massage the HTML a bit first:

// Convert all inline styles to old fashioned stuff to accomodate htmldocs:
	  $$template = str_replace('<span style="text-decoration: underline;">', '<u>', $$template);
	  $$template = str_replace('</span>', '</u>', $$template);
	  // Convert all <p> tags to <div> to remove padding and margins:
	  $$template = str_replace("<p", "<div", $$template);
	  $$template = str_replace("</p", "</div", $$template);
	  // Convert all empty <div>'s to line breaks:
	  $$template = str_replace("<div></div>", "", $$template);
	  $$template = str_replace("<div>&nbsp;</div>", "", $$template);
	  // Convert inline styles to deprecated alignment properties:
	  $$template = str_replace('style="text-align: justify;"', 'align="justify"', $$template);
	  $$template = str_replace('style="TEXT-ALIGN: justify"', 'align="justify"', $$template);
	  $$template = str_replace('style="text-align: right;"', 'align="right"', $$template);
	  $$template = str_replace('style="TEXT-ALIGN: right"', 'align="right"', $$template);
	  $$template = str_replace('style="text-align: center;"', 'align="center"', $$template);
	  $$template = str_replace('style="TEXT-ALIGN: center"', 'align="center"', $$template);
	  $$template = str_replace('style="text-align: left;"', 'align="left"', $$template);
	  $$template = str_replace('style="TEXT_ALIGN: left"', 'align="left"', $$template);

Cleverer people than I can probably do that in one line of regular expressions.

NettSite
Junior Poster in Training
56 posts since Feb 2010
Reputation Points: 15
Solved Threads: 13
 
chrishea
Nearly a Posting Virtuoso
1,428 posts since Sep 2008
Reputation Points: 210
Solved Threads: 230
 

If you do need to use deprecated forms as NettSite describes, this would be slightly quicker:

$srch = array('<span style="text-decoration: underline;">', '</span>', "<p", "</p", 'style="text-align: ', 'style="TEXT-ALIGN: ');
$repl =array ('<u>', '</u>', "<div", "</div", 'align="', 'align="');
$$template = str_replace($srch, $repl, $$template);
// Convert all empty <div>'s to line breaks:
$$template = str_replace(array("<div></div>", "<div>&nbsp;</div>"),array("", ""), $$template);


I've retained $$template, as I haven't seen the rest of the code, but normally I wouldn't use that. $template is normally correct. But if $template contains the name of a variable which contains the actual content to be worked on, then $$template is correct.

tiggsy
Junior Poster
119 posts since Jul 2009
Reputation Points: 16
Solved Threads: 8
 

this entry is blank

tiggsy
Junior Poster
119 posts since Jul 2009
Reputation Points: 16
Solved Threads: 8
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: