渲染的XHMTL生成一个空的pdf文件

时间:2017-09-09 22:39:46

标签: java pdf xhtml xhtmlrenderer

我正在使用ITextRenderer()方法将xhtml代码转换为java中的pdf。

这是我的代码:

ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(XHTML.toString());

renderer.layout();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
    renderer.createPDF(baos);
} catch (DocumentException e) {
    baos.close();
    setErrorMessage(response, 500, "Internal Server Error", "Could not create the PDF version of the patient summary.");
    return;
} 

 //convert PDF to base64
 String base64PDF = Base64.encodeBytes(baos.toByteArray());
 baos.close();  

我的XMTML.toString()包含以下内容:

 <html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
<style>
*{
  font-family: Arial;
}
table {
    width:100%;
}
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
th, td {
    padding: 5px;
    text-align: left;
}
table th    {
    background-color: #77f;
    color: white;
}


</style>
</head>
<body><h1>Patient Summary</h1><hr></hr><table><thead><tr><th colspan="2">Demographics</th></tr></thead><tbody><tr><td>Full Name</td><td>George Georgiou</td></tr><tr><td>Date of 
Birth</td><td>1990-01-01</td></tr><tr><td>Gender</td><td>Male</td></tr><tr><td>Address</td><td>31 Agiou Marona, 1010, Lefkosia, 
CY</td></tr></tbody></table><h3>Allergies</h3><table><thead><tr><th>Reaction Type</th><th>Clinical Manifestation</th><th>Agent</th><th>Onset Date</th></tr></thead><tbody><tr 
ID="allergy.1"><td ID="allergy.1.type">Panadol</td><td ID="allergy.1.manifestation">Dyspnoea</td><td ID="allergy.1.agent">Penicillamine and similar agents</td><td 
ID="allergy.1.onset">2017-07-04</td></tr></tbody></table>

不幸的是,创建的pdf文件为空。当我使用XHMTL代码进行在线转换以将其转换为pdf文件时,它已成功转换。

你能帮帮我吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

我的xhtml文件应包含<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"><title>部分。

我的完整xhtml文件如下:

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Patient Summary PDF</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
{
  font-family: Arial;
}
table {
    width:100%;
}
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
th, td {
    padding: 5px;
    text-align: left;
}
table th    {
    background-color: #77f;
    color: white;
}
</style>
</head>
<body><h1>Patient Summary</h1><table><thead><tr><th colspan="2">Demographics</th></tr></thead><tbody><tr><td>Full Name</td><td>George Georgiou</td></tr><tr><td>Date of Birth</td><td>1990-01-01</td></tr><tr><td>Gender</td><td>Male</td></tr><tr><td>Address</td><td>31 Agiou Marona, 1010, Lefkosia, CY</td></tr></tbody></table><h3>Allergies</h3><table><thead><tr><th>Reaction Type</th><th>Clinical Manifestation</th><th>Agent</th><th>Onset Date</th></tr></thead><tbody><tr ID="allergy.1"><td ID="allergy.1.type">Panadol</td><td ID="allergy.1.manifestation">Dyspnoea</td><td ID="allergy.1.agent">Penicillamine and similar agents</td><td ID="allergy.1.onset">2017-07-04</td></tr></tbody></table></body></html>

我必须添加三个额外的jar个文件:(a)core-renderer.jar,(b)core-renderer-minimal.jar和(c)xml-apis-xerces-2.9.1.jar

仅使用 iText-2.0.8.jar 文件提及所有这些工作非常重要。

此外,我必须导入import com.lowagie.text.DocumentException;import org.xhtmlrenderer.pdf.ITextRenderer;

相关问题