如何使用PDFTron(XSL Java)在html2pdf中渲染bootstrap css

时间:2017-08-21 11:39:19

标签: html xslt pdftron

我在java我的应用程序中使用PDFtron.jar将html转换为pdf除了bootstrap css在转换时不渲染(PDFTron在转换为pdf时不理解bootstrap css)时,每个东西都正常工作,因为例如下面的代码不被理解通过pdftron,我在我的pdf中获得了两个div。

<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>

以下是转换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());
            }
        } 

我的XSL html头

  <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>

1 个答案:

答案 0 :(得分:0)

如果内存服务,html2pdf无法获取外部资源。您需要将引导程序CSS文件和字体CSS文件的内容嵌入到某些<style>标记中