0

I am using PDFtron.jar in java my application to convert html to pdf every thing is working fine except bootstrap css are not rendering while converting (PDFTron do not understand bootstrap css while converting into pdf) for eg below code is not understood by pdftron and I am getting both div in my pdf.

<div class="col-xs-12 info-details visible-xs">Visible for xs screen</div>
<div class="col-xs-12 info-details hidden-xs">Visible for non xs screen</div>

Below is the code to convert htmlToPdf

    if (System.getProperty("java.library.path").indexOf(support.config.getTomcatHome() + File.separator + "lib") == -1)
            {
                System.setProperty("java.library.path", System.getProperty("java.library.path") + File.pathSeparatorChar + support.config.getTomcatHome() + File.separator
                        + "lib" + File.pathSeparatorChar + support.config.getTomcatHome() + File.separator
                        +  "lib"  + File.separator + "PDFNet.jar");
                java.lang.reflect.Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
                fieldSysPath.setAccessible(true);
                fieldSysPath.set(null, null);
                Util.Log("PDFNet.jar loaded");
            }

            OS = System.getProperty("os.name").toLowerCase();
            PdfViewerLicense.initialize();
            PDFNet.setTempPath(pdfFolderPath+File.separator);
            doc = new PDFDoc();
            converter = new HTML2PDF();
            if(OS.indexOf("win") < 0)
            {
                HTML2PDF.setModulePath(support.config.getTomcatHome() + File.separator + "lib"  + File.separator +"html2pdf.so" ); 
            }
            converter.setLandscape(false);
            converter.setPaperSize(PrinterMode.e_A4);
            converter.insertFromHtmlString(html);
            if (converter.convert(doc)){
                doc.save(output_pdf_path, SDFDoc.e_linearized, null);
                result = true;
            }else {
                Util.Log("PDFTron Conversion failed. HTTP Code: " + converter.getHTTPErrorCode() + "\n" + converter.getLog());
            }
        } 

My XSL html head

  <HEAD>
    <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://fonts.googleapis.com/css?family=Lato:400,400i,700" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro" rel="stylesheet">
    <TITLE> PDFTron </TITLE>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  </HEAD>
pise
  • 849
  • 6
  • 24
  • 51

1 Answers1

0

If memory serves, html2pdf can't grab external resources. You'll need to embed the contents of the bootstrap CSS file and the font CSS files inside some <style> tags

Scoots
  • 3,048
  • 2
  • 21
  • 33
  • AH, my bad. I thought I was in the PHP tag when I saw the question. Just use the Java equivilent code - https://stackoverflow.com/questions/2586975/how-to-use-curl-in-java – Scoots Aug 21 '17 at 11:49
  • Sorry but I didn't mention this style is written in XSL file – pise Aug 21 '17 at 11:51
  • Well, you need some way of getting the contents of that css file into your document before html2pdf picks up on it. That's what I believe to be the solution within the scope of your question. Can you find/replace a string? Actually just manually copy and paste the contents into your template file? – Scoots Aug 21 '17 at 12:06