• Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

HTML to PDF java lib

minestrone

More fingers than teeth
Joined
Apr 24, 2008
Messages
17,844
Likes received
990
Thanks received
182
I have to do a print friendly PDF for a set of dynamic HTML pages. Have done this in the past but not for many years so I am out the loop.

I am wanting to trap the request, pick up the HTML from the DB, pass it to a library which will sort out wrapping and such like then push the created PDF down the reponse pipe to a pop up.

Anyone know of anything that will handle this? Does not have to be anything fancy, cheap and easy are the watchwords on this job (just like my tarts).

Cheers
 
HTML to PDF

HTML to PDF

I have to do a print friendly PDF for a set of dynamic HTML pages. Have done this in the past but not for many years so I am out the loop.

I am wanting to trap the request, pick up the HTML from the DB, pass it to a library which will sort out wrapping and such like then push the created PDF down the reponse pipe to a pop up.

Anyone know of anything that will handle this? Does not have to be anything fancy, cheap and easy are the watchwords on this job (just like my tarts).

Cheers

You can convert HTML to PDF using the web service DocRaptor.com. They can also convert xml to excel, though I'm not sure that'll be useful for you. They have a bunch of examples in various languages that you might be able to use.
 
I have to do a print friendly PDF for a set of dynamic HTML pages. Have done this in the past but not for many years so I am out the loop.

I am wanting to trap the request, pick up the HTML from the DB, pass it to a library which will sort out wrapping and such like then push the created PDF down the reponse pipe to a pop up.

Anyone know of anything that will handle this? Does not have to be anything fancy, cheap and easy are the watchwords on this job (just like my tarts).

Cheers

itextpdf is the tool I always seem to end up with for doing this stuff. The commercial rates are high tho if you need to give it to others (LGPL licence).

The other option is pd4ml which is 100 euros or so.
 
The OP seems to be ignoring all these helpful suggestions, perhaps we shouldn't bother next time :)
 
I have been running around all week, 14 hour days just now. This was going to be a saturday job.

I'm going to try the apache fop first off, i seem to remeber using that round about 2003. I have used jtidy so it seems a resonable fit, and I don't want to pay. The transform does not do tables but I can live with that.
 
Actually I text looks pretty alright, might give that a whirl today.
 
Eventually got round to getting a test harness working with iText using the XMLworker add on.

And if I post it here from clientCo I can pick it up tonight and stick it into Plan B :smile

Cheers

Code:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

/**
 * 
 * @author XXXX
 *
 */
public class Test {

	/**
	 * 
	 * @param args
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static void main(String[] args) throws IOException,
			DocumentException {

		Document document = new Document();
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PdfWriter writer = PdfWriter.getInstance(document, baos);

		document.open();

		String text = new String( "<html><head></head><body>" +
				                  "<h1>Title</h1><p>Paragraph</p>" +
				                  "</body></html>");

		InputStream inputHtmlAsStream = new ByteArrayInputStream(text
				.getBytes("UTF-8"));

		XMLWorkerHelper.getInstance().parseXHtml(writer, document,
				inputHtmlAsStream, null);

		document.close();


		System.out.println(baos.toString());

	}
}
 

Advertisers

Back
Top