HTML to PDF java lib
+ Reply to Thread
Posts 1 to 8 of 8
  1. #1

    Banned


    Join Date
    Apr 2008
    Posts
    15,490
    Thanks (Given)
    4
    Thanks (Received)
    102
    Likes (Given)
    6
    Likes (Received)
    595

    Default HTML to PDF java lib

    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

  2. #2

    More time posting than coding


    Join Date
    Jun 2011
    Posts
    454
    Thanks (Given)
    1
    Thanks (Received)
    14
    Likes (Given)
    32
    Likes (Received)
    38

    Default

    Apache FOP ?

    Will need a bit of work converting HTML to a suitable input format for the FOP processor :-

    HowTo/HtmlToPdf - Xmlgraphics-fop Wiki

  3. #3

    Nervous Newbie


    Join Date
    Aug 2011
    Posts
    1
    Thanks (Given)
    0
    Thanks (Received)
    0
    Likes (Given)
    0
    Likes (Received)
    0

    Default HTML to PDF

    Quote Originally Posted by minestrone View Post
    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.

  4. #4
    eek
    eek is offline

    bored now

    eek's Avatar
    Join Date
    Jun 2010
    Location
    😂
    Posts
    22,738
    Thanks (Given)
    272
    Thanks (Received)
    1299
    Likes (Given)
    1086
    Likes (Received)
    3781

    Default

    Quote Originally Posted by minestrone View Post
    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.
    merely at clientco for the entertainment

  5. #5

    More time posting than coding


    Join Date
    Jun 2011
    Posts
    454
    Thanks (Given)
    1
    Thanks (Received)
    14
    Likes (Given)
    32
    Likes (Received)
    38

    Default

    The OP seems to be ignoring all these helpful suggestions, perhaps we shouldn't bother next time

  6. #6

    Banned


    Join Date
    Apr 2008
    Posts
    15,490
    Thanks (Given)
    4
    Thanks (Received)
    102
    Likes (Given)
    6
    Likes (Received)
    595

    Default

    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.

  7. #7

    Banned


    Join Date
    Apr 2008
    Posts
    15,490
    Thanks (Given)
    4
    Thanks (Received)
    102
    Likes (Given)
    6
    Likes (Received)
    595

    Default

    Actually I text looks pretty alright, might give that a whirl today.

  8. #8

    Banned


    Join Date
    Apr 2008
    Posts
    15,490
    Thanks (Given)
    4
    Thanks (Received)
    102
    Likes (Given)
    6
    Likes (Received)
    595

    Default

    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

    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());
    
    	}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Content Relevant URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.