提问人:Altan Mehmet Türkmen 提问时间:1/3/2023 最后编辑:ChristopherAltan Mehmet Türkmen 更新时间:4/17/2023 访问量:155
无法在Spring Boot,Java中使用Apache速度和itextpdf将页面从html水平拆分为pdf转换
Can't split the page horizontally from html to pdf conversion with Apache velocity and itextpdf in Spring Boot,Java
问:
我有一个函数可以从数据库中获取数据并将它们填充到准备好的 HTML 模板中,然后将它们导出到 pdf 文件。为此,我正在使用 Apache Velocity vm 文件和 itextpdf。我需要在一页上执行此操作,因为用户将打印 pdf 文件。由于 itextpdf 版本,用于拆分页面的传统 CSS 代码不起作用。
这是我的pom.xml:
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf.tool/xmlworker -->
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
这是我的控制器类,用于创建和下载 pdf 文件:
@GetMapping("/genpdf/{fileName}")
HttpEntity<byte[]> createPdf(
@PathVariable("fileName") String fileName) throws IOException {
/* first, get and initialize an engine */
VelocityEngine ve = new VelocityEngine();
/* next, get the Template */
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class",
ClasspathResourceLoader.class.getName());
ve.init();
Template t = ve.getTemplate("templates/helloworld.vm");
/* create a context and add data */
VelocityContext context = new VelocityContext();
context.put("name", "World");
context.put("genDateTime", LocalDateTime.now().toString());
/* now render the template into a StringWriter */
StringWriter writer = new StringWriter();
t.merge(context, writer);
/* show the World */
System.out.println(writer.toString());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos = generatePdf(writer.toString());
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_PDF);
header.set(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=" + fileName.replace(" ", "_"));
header.setContentLength(baos.toByteArray().length);
return new HttpEntity<byte[]>(baos.toByteArray(), header);
}
public ByteArrayOutputStream generatePdf(String html) {
String pdfFilePath = "";
PdfWriter pdfWriter = null;
// create a new document
Document document = new Document();
try {
document = new Document();
// document header attributes
document.addCreationDate();
document.addTitle("HTML to PDF using itext");
document.setPageCount(1);
document.addLanguage("tr");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pdfWriter = PdfWriter.getInstance(document, baos);
// open document
document.open();
pdfWriter.addPageDictEntry(PdfName.PDF, PdfPage.LANDSCAPE);
XMLWorkerHelper xmlWorkerHelper = XMLWorkerHelper.getInstance();
xmlWorkerHelper.parseXHtml(pdfWriter, document, new StringReader(
html));
// close the document
document.close();
System.out.println("PDF generated successfully");
return baos;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
这是我的test.vm文件,用于生成pdf文件:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//TR"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="tr">
<head>
<style >
p {
font-size: 10px;
}
span {
font-size: 10px
}
.split {
height: 100%;
width: 50%;
position: fixed;
z-index: 1;
top: 0;
overflow-x: hidden;
padding-top: 20px;
}
/* Control the left side */
.left {
left: 0;
background-color: #111;
}
/* Control the right side */
.right {
right: 0;
background-color: darkblue;
}
</style>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Sceda Techniqa</title>
</head>
<body>
<div class="split left">
<div style="display: flex;flex-direction: row">
<p>test</p>
<span><strong>test</strong></span>
</div>
<div style="display: flex;flex-direction: row">
<p>test</p>
</div>
</div>
</div>
<div class="split right">
<div style="display: flex;flex-direction: row">
<span><strong>test</strong></span>
</div>
<div style="display: flex;flex-direction: column">
<div style="display: flex;flex-direction: row">
<p>test</p>
<span><strong>2</strong></span>
</div>
<div style="display: flex;flex-direction: row">
<p>test</p>
<span><strong>4</strong></span>
</div>
</div>
</div>
</body>
</html>
我尝试为 Maven 使用不同的 HTML 到 pdf 库,但无法达到预期的结果。我需要的是一个完全可配置的 Java css 和 html 到 pdf 转换器。
非常感谢您的帮助。
答: 暂无答案
评论