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