想用Flying Saucer生成风景pdf

时间:2017-06-22 11:43:31

标签: itext flying-saucer

当我使用飞碟将其转换为PDF时,我在下面给出了html,它不适合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(); 
    }
}

1 个答案:

答案 0 :(得分:4)

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

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

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