[英]Flying Saucer not rendering PDF
I am converting HTML form to PDF with Flying-Saucer but the PDF output doesn't have all the formatting (CSS & Bootstrap Applied).我正在使用 Flying-Saucer 将 HTML 表单转换为 PDF,但 PDF 输出没有所有格式(应用 CSS 和引导程序)。 It is also clipping the side of the page.
它还剪裁了页面的一侧。
The form rendered in HTML looks like:
以 HTML 呈现的表单如下所示:
But the PDF comes out as:
但 PDF 格式如下:
From the above it looks like its getting the panes with rounded edges, but no colors, so I presume its lacking the css.从上面看起来它得到了带有圆边的窗格,但没有颜色,所以我认为它缺少 css。
The HTML relies on a external css file, boostrap css, and bootswatch css theme. HTML 依赖于外部 css 文件、boostrap css 和 bootswatch css 主题。 So the css references in the html are:
所以html中的css引用是:
<link rel="stylesheet" type="text/css" href="/css/assessment.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/flatly/bootstrap.min.css">
The java to generate the PDF using Flying Saucer is:使用飞碟生成 PDF 的 java 是:
@Qualifier("templateEngine")
@Autowired
private TemplateEngine templateEngine;
public void createPdf(String templateName, Map map) throws Exception {
Assert.notNull(templateName, "The templateName can not be null");
Context ctx = new Context();
if (map != null) {
Iterator itMap = map.entrySet().iterator();
while (itMap.hasNext()) {
Map.Entry pair = (Map.Entry) itMap.next();
ctx.setVariable(pair.getKey().toString(), pair.getValue());
}
}
String processedHtml = templateEngine.process(templateName, ctx);
FileOutputStream os = null;
//String fileName = UUID.randomUUID().toString();
String fileName = "Assessment.pdf";
try {
final File outputFile = File.createTempFile(fileName, ".pdf");
os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.getSharedContext().getUserAgentCallback().setBaseURL("/resources/static/css");
renderer.setDocumentFromString(processedHtml);
renderer.layout();
renderer.createPDF(os, false);
renderer.finishPDF();
I am not entirely sure that this path is correct:我不完全确定这条路径是否正确:
renderer.getSharedContext().getUserAgentCallback().setBaseURL("/resources/static/css");
Finally the project structure looks like:最后项目结构如下:
I had a similar problem when using Bootstrap 4.x.我在使用 Bootstrap 4.x 时遇到了类似的问题。 It seems you are using Bootstrap 4 as well:
看来您也在使用 Bootstrap 4:
https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css
My "clipped on right" problem went away when I switched to Bootstrap 3.x.当我切换到 Bootstrap 3.x 时,我的“右夹”问题消失了。
Most likely cause:最可能的原因:
Flying Saucer does not support 'Flex'.飞碟不支持“Flex”。 Bootstrap 4.x uses Flex.
Bootstrap 4.x 使用 Flex。 Bootstrap 3 does not.
Bootstrap 3 没有。
For me at least, everything worked perfectly when converting HTML to PDF when I switched to Bootstrap 3.x.至少对我来说,当我切换到 Bootstrap 3.x 时,将 HTML 转换为 PDF 时一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.