Want to generate landscape pdf using Flying Saucer

心已入冬 提交于 2021-02-08 08:41:31

问题


I am having below given html with me, when I am converting this to PDF using flying saucer, It is not fitting A4 Portrait size. How to generate landscape pdf file.

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

I am using below given code for converting html to 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:


You can set your page in landscape mode using CSS, using:

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

The page size is part of CSS 3 specification, and is supported by flying saucer.



来源:https://stackoverflow.com/questions/44698455/want-to-generate-landscape-pdf-using-flying-saucer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!