[geeks] Text to PDF (was Re: I need an HTML to PDF converter)

Dave Fischer dave at cca.org
Wed Feb 22 14:06:52 CST 2006


brian.dunbar at liftport.com writes:

>How odd.  I was looking a problem yesterday with similar intent.

>We have a form on a web page, currently PHP.  We'd like to take their
>Name, slam that into a PDF file with legal boiler plate around and fire
>it back at them - by their own request of course!

>It looks like a way would be to use PHP to build the text - with the
>variable buried in it - then use enscript to create the PS from text,
>then ps2pdf for the PDF file.

>PHP > *text_file* > enscript > *PS_file > PS2DF > *PDF_file*

>Making life complicated the boiler plate has it's own font, the headers
>another, and several sections are in bold with underline.

>Am I guilty of complicating this process?

I'd generate the postscript from the server script, rather than
going through a plain text intermediate format.

But I'm a postscript hacker, so obviously I'd do that.

Script-generated PS generally looks something like:


(
	cat header.ps

	while read text
	do
		echo "(${text}) my_text_fn"
	done

	echo showpage
) | ps2pdf > ....

Where header.ps has all the PS initialization stuff (selecting fonts, 
scaling, etc) as well as defining "my_text_fn" which outputs a line of
text, drops down a line, and checks to see if you've hit the end of the
page, dealing with the details of starting a new page if necessary.

You generally have to massage ${text} a little bit - escape special
characters (parentheses), etc.

HOWEVER.

If the variable text is limited to a couple of fields, it's much
easier to generate a postscript file of the boilerplate, with
easily grepable placeholders for the variable text fields, and then
swap in the real data using sed.

cat boilerplate.ps | sed "s/FIRST_NAME/${FIRST_NAME}/" | ps2pdf > ....

Etc.

----- David Fischer ------- dave at cca.org ------- http://www.cca.org ------
-- Being poked in the eye with a pointy stick makes the baby Jesus cry. --



More information about the geeks mailing list