0

我正在使用Flying Saucer APIwithiText PDF将 HTML 内容转换为 PDF。

这需要以下库:

  • 核心渲染器.jar
  • iText-2.0.8.jar

由于该库不支持input type checkbox,所以我使用checkbox imagePDF 进行渲染。

但是,图像没有出现。它什么也没显示。

中的资源flyingsaucer-R8.zip

示例

 StringBuilder myHtml = new StringBuilder(); 
 myHtml.append("<html><img src=\"images/invoice-bg.jpg\"></img></html>");
// if you have html source in hand, use it to generate document object
Document document = XMLResource.load( new ByteArrayInputStream( 
myHtml.toString().getBytes() ) ).getDocument();

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument( document, null );

renderer.layout();

String fileNameWithPath = "D:/Temp/PDF-XhtmlRendered.pdf";
FileOutputStream fos = new FileOutputStream( fileNameWithPath );
renderer.createPDF( fos );
fos.close();
System.out.println( "File 1: '" + fileNameWithPath + "' created." );
4

3 回答 3

0

给出图像的完整路径,然后它就起作用了。

我通过直接点击以下 URL 进行了检查:

http://localhost:8001/userApi/resources/images/invoice-bg.jpg

如果遇到类似问题,这可能会对某人有所帮助-

 StringBuilder myHtml = new StringBuilder(); 
 myHtml.append("<html><img 
 src=\""+serverName+"/userApi/resources/images/invoice-bg.jpg\" 
 style=\"margin-top:-4px;\" ></img></html>");
// if you have html source in hand, use it to generate document object
Document document = XMLResource.load( new ByteArrayInputStream( 
myHtml.toString().getBytes() ) ).getDocument();

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument( document, null );

renderer.layout();

String fileNameWithPath = "D:/Temp/PDF-XhtmlRendered.pdf";
FileOutputStream fos = new FileOutputStream( fileNameWithPath );
renderer.createPDF( fos );
fos.close();
System.out.println( "File 1: '" + fileNameWithPath + "' created." );
于 2017-12-27T14:33:10.540 回答
0

如果您使用具有相对文件路径的资源,则需要在setDocument调用中指定一个基本 URL。

于 2017-12-24T13:48:27.747 回答
0

您应该使用最新版本的飞碟(当前为 9.1.9)。您可以在mvnrepository上找到。

您还应该检查该文件是否images/invoice-bg.jpg存在并且可以从项目的根目录访问。

如果您的路径正确,则生成的 PDF 将包含您的图像。

于 2017-12-18T16:46:19.460 回答