【问题标题】:Want to generate landscape pdf using Flying Saucer想用飞碟生成风景pdf
【发布时间】:2017-11-25 16:15:28
【问题描述】:

我有下面给出的 html,当我使用飞碟将其转换为 PDF 时,它不适合 A4 纵向尺寸。如何生成横向pdf文件。

<html>

<head>
  <title>
    Selenium Test SC
  </title>
</head>

<body style='font-family:Tahoma;font-size:9pt;letter-spacing:0.5px'>
  <table border='1' style='color:black;font-size:12px;'>
    <tr>
      <td>
        <a href="file:///C:/Users/611066167/Documents/Sel%20Test%20Result/Screenshots/Screen1.png" target="_blank">
NGSD:TMD (Search)
<img src="file:///C:/Users/611066167/Documents/Sel%20Test%20Result/Screenshots/Screen1.png" title="NGSD:TMD (Search)" style="width:100%;height:100%" alt="NGSD:TMD (Search)" border="0">
</a>
      </td>
    </tr>
  </table>
</body>

</html>

我正在使用下面给出的代码将 html 转换为 pdf

inputFile = "C:/Users/611066167/Documents/Sel Test Result/screenfile.html"; 
outputFile = "C:/Users/611066167/Documents/Sel Test Result/screenfile.pdf"; 
generatePDF(inputFile, outputFile); 


public void generatePDF(String inputHtmlPath, String outputPdfPath)
{
    try
    {     
        String url = new File(inputHtmlPath).toURI().toURL().toString(); 
        System.out.println("URL: " + url);
        OutputStream out = new FileOutputStream(outputPdfPath); 

        // Flying Saucer part 
        ITextRenderer renderer = new ITextRenderer();   
        renderer.setDocument(url);
        renderer.layout(); 
        renderer.createPDF(out); 

        out.close();
    } 
    catch (DocumentException | IOException e)     
    { 
        // TODO Auto-generated catch block e.printStackTrace(); 
    }
}

【问题讨论】:

  • 飞碟不是 iText 产品。如果要使用 iText 将 HTML 转换为 PDF,则需要使用 pdfHTML:itextpdf.com/blog/pdfhtml-configuration-options 使用接受 PdfDocument 实例的方法,并在 PdfDocument 级别定义页面大小。

标签: itext flying-saucer


【解决方案1】:

您可以使用 CSS 将页面设置为横向模式,使用:

<style>
   @page { size: A4 landscape;}
</style>

页面大小是 CSS 3 规范的一部分,由飞碟支持。

【讨论】:

    猜你喜欢
    • 2016-06-26
    • 2021-03-25
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 2016-08-31
    • 2018-03-22
    相关资源
    最近更新 更多