使用 FlyingSaucer/iTExt5 生成的 PDF 为空白
Pdf produced using FlyingSaucer/iTExt5 is blank
我正在尝试从 html 字符串生成 PDF 并收到响应,但文档未显示在 PDF 查看器中。我似乎遗漏了一些东西,回复是损坏的 PDF。如果我能得到任何建议,将不胜感激。
我的 pom.xml
中包含以下内容
<!-- dependencies for saving as PDF -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>9.1.16</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-itext5</artifactId>
<version>9.1.6</version>
</dependency>
我的端点是一个控制器,代码如下所示:
@POST
@Produces({"application/pdf"})
@Path("/renderPDF")
public void renderPDF(@Context HttpServletRequest request, @Context HttpServletResponse response) throws WebAppException {
try {
String = "<html><body>some content</body></html>";
RenderPDF.createPDF(response, sb);
}
catch (Exception e) {
LOGGER.error("renderPDF", e);
}
}
createPDF 定义如下:
public class RenderPDF {
public static void createPDF(HttpServletResponse response, String html)
throws IOException, DocumentException, ParserConfigurationException, SAXException {
try {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
renderer.createPDF(baos);
// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
// the contentlength
response.setContentLength(baos.size());
OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
os.close();
} catch(Exception e) {
}
}
问题出在我的 angular http 拦截器中;需要将响应类型设置为 arrayBuffer。
我正在尝试从 html 字符串生成 PDF 并收到响应,但文档未显示在 PDF 查看器中。我似乎遗漏了一些东西,回复是损坏的 PDF。如果我能得到任何建议,将不胜感激。
我的 pom.xml
中包含以下内容 <!-- dependencies for saving as PDF -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>9.1.16</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-itext5</artifactId>
<version>9.1.6</version>
</dependency>
我的端点是一个控制器,代码如下所示:
@POST
@Produces({"application/pdf"})
@Path("/renderPDF")
public void renderPDF(@Context HttpServletRequest request, @Context HttpServletResponse response) throws WebAppException {
try {
String = "<html><body>some content</body></html>";
RenderPDF.createPDF(response, sb);
}
catch (Exception e) {
LOGGER.error("renderPDF", e);
}
}
createPDF 定义如下:
public class RenderPDF {
public static void createPDF(HttpServletResponse response, String html)
throws IOException, DocumentException, ParserConfigurationException, SAXException {
try {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
renderer.createPDF(baos);
// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
// the contentlength
response.setContentLength(baos.size());
OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
os.close();
} catch(Exception e) {
}
}
问题出在我的 angular http 拦截器中;需要将响应类型设置为 arrayBuffer。