Viewing 1 post (of 1 total)
  • Author
    Posts
  • #26937

    Hi, I am pd4ml rookie here. So, please forgive me if I am asking a primeval question.
    I am trying to integrate pd4ml with phantomjs to generate a pdf from html that contains javascript. So far, I was unable to generate for a simple html using the web module. I searched for examples but could not find anything to my satisfaction. I am posting my actual code here.
    This is my java code(taken from the toc example provided in the downloads). The only change I made(as mentioned in https://pd4ml.com/pd4ml_web.htm) was to use the web.pd4ml package. I am using the same sample.html file that comes with the example.

    package main;
    
    import java.awt.Dimension;
    import java.awt.Insets;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    import org.zefer.pd4ml.PD4Constants;
    import org.zefer.pd4ml.PD4PageMark;
    import org.zefer.pd4ml.web.PD4ML;
    
    public class WebPageConverterWithPhantomJS {
    
    public static void main(String[] args) throws Exception {
    WebPageConverterWithPhantomJS converter = new WebPageConverterWithPhantomJS();
    String htmlFileName = "C:\DEV\workspace\PD4MLExample\examples\toc\sample.html";
    File pdfFile = new File("C:\DEV\workspace\PD4MLExample\examples\toc\sampleUsingPhantomJS.pdf");
    converter.generatePDF(htmlFileName, pdfFile, PD4Constants.A4,
    args.length > 2 ? args[2] : null, args.length > 3 ? args[3] : null );
    System.out.println("done.");
    }
    
    private void generatePDF(String inputHTMLFileName, File outputPDFFile, Dimension format, String fontsDir, String headerBody)
    throws Exception {
    
    java.io.FileOutputStream fos = new java.io.FileOutputStream(outputPDFFile);
    PD4ML pd4ml = new PD4ML("C:\DEV\workspace\PD4MLExample\public\tools\phantomjs.exe");
    pd4ml.setPageInsets(new Insets(20, 10, 10, 10));
    pd4ml.setHtmlWidth(950);
    pd4ml.setPageSize(pd4ml.changePageOrientation(format)); // landscape page orientation
    if ( fontsDir != null && fontsDir.length() > 0 ) {
    pd4ml.useTTF( fontsDir, true );
    }
    if ( headerBody != null && headerBody.length() > 0 ) {
    PD4PageMark header = new PD4PageMark();
    header.setAreaHeight( -1 ); // autocompute
    header.setHtmlTemplate( headerBody ); // autocompute
    pd4ml.setPageHeader( header );
    }
    pd4ml.enableDebugInfo();
    
    InputStream is = new FileInputStream(inputHTMLFileName);
    InputStreamReader isr = new InputStreamReader(is, "UTF-8");
    
    pd4ml.render(isr, fos);
    //pd4ml.render("file:" + inputHTMLFileName, fos);
    }
    }

    This is the change I made in the html . Included bootstrap css, font-awesome css, jquery and bootstrap js files.

    <META http-equiv=Content-Type content="text/html; charset=UTF-8">
    ..
    <LINK href="i/W3C-REC.css" type=text/css rel=stylesheet>
    <LINK href="i/default.css" type=text/css rel=STYLESHEET>
    <link rel="stylesheet" type="text/css" href="public/stylesheets/bootstrap-2.3.css"/>
    <link rel="stylesheet" type="text/css" href="public/stylesheets/font-awesome.css" />
    .
    .
    <script type="text/javascript" src="public/javascripts/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="public/javascripts/bootstrap-2.1.min.js"></script>
    .
    .
    </HEAD>
    <BODY>
    
    <script type="text/javascript">
    $(document).ready(function(){
    $(".thisred").addClass("red"); //testing only.
    });
    </script>
    
    ...
    </BODY>

    When I execute this code, it throws the error

    090b2 Pro (eval)
    ReferenceError: Can't find variable: $
    
    c:%5CUsers%5CUKALIVI%5CAppData%5CLocal%5CTemp%5Cpd4ml1002373815533658363.htm:29
    Exception in thread "main" java.lang.NoSuchMethodError: org.zefer.pd4ml.pdf.parser.h.o00000(Ljava/io/InputStream;)[B
    at org.zefer.pd4ml.web.PD4ML.Ò00000(Unknown Source)
    at org.zefer.pd4ml.web.PD4ML.render(Unknown Source)
    at main.WebPageConverterWithPhantomJS.generatePDF(WebPageConverterWithPhantomJS.java:50)
    at main.WebPageConverterWithPhantomJS.main(WebPageConverterWithPhantomJS.java:23)

    If I change the instantiation of pd4ml to :

    PD4ML pd4ml = new PD4ML("C:\DEV\workspace\PD4MLExample\public\tools\phantomjs.exe");

    I don’t get the error and the console shows done. But the pdf generated is not useful, the pd4ml tags were not recognized I think.

    090b2 Pro (eval)
    ReferenceError: Can't find variable: $
    
    c:%5CUsers%5CUKALIVI%5CAppData%5CLocal%5CTemp%5Cpd4ml2710392443226086703.htm:29
    done.

    Could you please let me know what I am missing here. Also, could you please provide some examples for integration with phantomjs.

Viewing 1 post (of 1 total)

The forum ‘General questions / FAQ’ is closed to new topics and replies.