【问题标题】:Flying-saucer/iText PDF not finding css file飞碟/iText PDF 找不到 css 文件
【发布时间】:2016-11-29 15:19:59
【问题描述】:

我可以使用飞碟/iText 生成 PDF。但是,我不知道如何让样式表包含在 PDF 渲染中。

我已将此flying-saucer/iText PDF in servlet not finding css file 用作参考。

涉及多个 css 文件,因此我将无法使用 renderer.setDocument(doc, "http://example.com/something/page.html"); 作为解决方案

我已经实现了一个类似于提问者使用的东西,但它返回了 Caused by: java.nio.file.InvalidPathException: Illegal char <:> at index 4: http://localhost:8080/proj/resources/css/custom1.css 错误

这是我的代码

      StringBuilder bui = new StringBuilder();
      bui.append("<html><head><style>");
      bui.append(readFile(path+"/resources/css/custom1.css", Charsets.UTF_8));
      bui.append(readFile(path+"/resources/css/custom2.css", Charsets.UTF_8));
      bui.append(readFile(path+"/resources/css/custom3.css", Charsets.UTF_8));

      bui.append("</style></head>");
      bui.append("<body><div><table>");
      bui.append( xhtml_file );
      bui.append("</table></div></body></html>");

      InputStream stream = new ByteArrayInputStream(bui.toString().getBytes( StandardCharsets.UTF_8 ));
      Document doc = tidy.parseDOM(stream, null);
      File outputFile = new File(directory+ "FILENAME" +".pdf");
      os = new FileOutputStream(outputFile);
      ITextRenderer renderer = new ITextRenderer();
      renderer.setDocument(doc, null);
      renderer.layout();
      renderer.createPDF(os, false);
      renderer.finishPDF();

传递的路径是“http://localhost:8080” 如果我直接在地址栏中输入“http://localhost:8080/resources/css/custom1.css”,它会显示 css 文件。我尝试删除“路径”,但它也没有得到 css。我做错了什么?

【问题讨论】:

    标签: css pdf itext flying-saucer


    【解决方案1】:

    尝试使用file:/// url协议来指向(用三个斜线)直接指向css文件。

    【讨论】:

      猜你喜欢
      • 2012-04-01
      • 1970-01-01
      • 2019-01-07
      • 2013-07-30
      • 2021-03-25
      • 2017-08-04
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多