在maven项目中没有使用Flying Saucer API显示图像

问题描述 投票:0回答:3

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

这需要以下库:

  • 核心renderer.jar
  • iText的 - 2.0.8.jar

由于该库不支持input type checkbox所以我使用checkbox image以PDF格式呈现。

但是,图像不会出现。它没有任何表现。

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." );
java maven pdf flying-saucer xhtmlrenderer
3个回答
0
投票

您应该使用最新版本的Flying-Saucer(目前为9.1.9)。你可以在mvnrepository找到。

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

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


0
投票

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


0
投票

给出图像的完整路径然后它工作。

我通过直接点击以下网址检查了这个:

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." );
© www.soinside.com 2019 - 2024. All rights reserved.