2017-08-21 70 views
0

我在java中使用PDFtron.jar我的應用程序將html轉換爲pdf每個東西都工作正常,除了bootstrap css轉換時不呈現(PDFTron不明白引導程序css轉換成pdf),例如下面的代碼pdftron不理解,我得到我的PDF格式的兩個div。如何在html2pdf中使用PDFTron呈現bootstrap css(XSL Java)

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

回答

0

如果沒記錯,HTML2PDF不能搶外部資源。您需要將引導程序CSS文件和字體CSS文件的內容嵌入一些​​<style>標記

+0

我正在使用java,那麼php將在此處工作? – pise

+0

啊,我的不好。當我看到問題時,我以爲我在PHP標籤中。只需使用Java的equivilent代碼 - https://stackoverflow.com/questions/2586975/how-to-use-curl-in-java – Scoots

+0

對不起,但我沒有提到這種風格是寫在XSL文件 – pise