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

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

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

    If I change the instantiation of pd4ml to :
    <br /> PD4ML pd4ml = new PD4ML("C:\DEV\workspace\PD4MLExample\public\tools\phantomjs.exe");<br />

    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.

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

    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.