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?


Recommended Answers

All 7 Replies

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

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.

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

// 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

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

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>", "<br>", $$template);
	  $$template = str_replace("<div>&nbsp;</div>", "<br>", $$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.

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("<br>", "<br>"), $$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.

this entry is blank

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.