Showing posts with label HTML to PDF using Java. Show all posts
Showing posts with label HTML to PDF using Java. Show all posts

Thursday, 12 September 2013

HTML to PDF using Java

1. Grab html source using javascript function

function getPageHTML(){ 
                return "<html>" + document.getElementsByTagName('html')[0].innerHTML + "</html>";
            }
2. Post this html source as parameter using jquery on click of pdfDownload.
printPDF is relative url i.e http:localhost:8080/Demo/printPDF

htmlString is post request parameter

$("#pdfDownload").click(function() {
                //alert("Handler for .click() called.");

                var fullHtml = getPageHTML();

                $.post("printPDF", {
                    htmlString : fullHtml
                   
                });

            });

3. Save html source to .html file and convert html to pdf using YAHP Converter. Here is example of action class of struts2.
package com.doctorAppointment.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.allcolor.yahp.converter.CYaHPConverter;
import org.allcolor.yahp.converter.IHtmlToPdfTransformer;
import org.apache.struts2.ServletActionContext;

public class PrintPDF {

    private InputStream fileInputStream;

    public InputStream getFileInputStream() {
        return fileInputStream;
    }

    // configure fopFactory as desired
    // private final FopFactory fopFactory = FopFactory.newInstance();

    /** xsl-fo namespace URI */
    protected static String foNS = "http://www.w3.org/1999/XSL/Format";

    public String execute() throws Exception {

        System.out.println("PrintPDF start");
        HttpServletRequest request = ServletActionContext.getRequest();
        String html = request.getParameter("htmlString");
        String contextFolder = request.getServletContext().getRealPath("");

        String htmlFileName = contextFolder + "\\download.html";
        String pdfFileName = contextFolder + "\\download.pdf";

        File htmlFile = new File(htmlFileName);
        if (htmlFile.exists()) {
            htmlFile.delete();
            htmlFile.createNewFile();
        } else {
            htmlFile.createNewFile();
        }
        // save html
        FileWriter htmlFileWriter = new FileWriter(htmlFile);
        htmlFileWriter.write(html);
        htmlFileWriter.flush();
        htmlFileWriter.close();

        // File pdfFile=new File(pdfFileName);

        // new converter
        CYaHPConverter converter = new CYaHPConverter();
        // save pdf in outfile
        File fout = new File(pdfFileName);
        FileOutputStream out = new FileOutputStream(fout);
        // contains configuration properties
        Map properties = new HashMap();
        // list containing header/footer
        List headerFooterList = new ArrayList();
        // add header/footer
        headerFooterList
                .add(new IHtmlToPdfTransformer.CHeaderFooter(
                        "<table width=\"100%\"><tbody><tr><td align=\"left\">"
                                + "Generated with YaHPConverter.</td><td align=\"right\">Page <pagenumber>/<"
                                + "pagecount></td></tr></tbody></table>",
                        IHtmlToPdfTransformer.CHeaderFooter.HEADER));
        headerFooterList.add(new IHtmlToPdfTransformer.CHeaderFooter(
                "© 2009 Quentin Anciaux",
                IHtmlToPdfTransformer.CHeaderFooter.FOOTER));
        properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS,
                IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER);
        // properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, fontPath);

        converter.convertToPdf(htmlFile.toURI().toURL(),
                IHtmlToPdfTransformer.A4P, headerFooterList, out, properties);

        out.flush();
        out.close();

        fileInputStream = new FileInputStream(fout);
        return "SUCCESS";
    }

}

Required YAHP maven dependency
<dependency>
            <groupId>com.google.code.maven-play-plugin.org.allcolor.yahp</groupId>
            <artifactId>yahp</artifactId>
            <version>1.3</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.maven-play-plugin.org.allcolor.yahp</groupId>
            <artifactId>yahp-internal</artifactId>
            <version>1.3-patched-play-pdf-0.8</version>
        </dependency>

Source : https://github.com/milandashara/struts2-jasper-yahp-demo.git

Note : Download may not run from eclipse. It will work after deploying war in tomcat