2017-09-09 2 views
0

Ich verwende ITextRenderer() Methode, um einen XHTML-Code in java in pdf zu konvertieren.XHMTL gerendert erzeugt eine leere PDF-Datei

Dies ist mein Code:

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(); 

Mein XMTML.toString() enthält folgende Komponenten:

<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> 

Leider ist die erstellte PDF-Datei leer ist. Wenn ich den XHMTL-Code für eine Online-Konvertierung verwende, um ihn in eine PDF-Datei zu konvertieren, wird er erfolgreich konvertiert.

Können Sie mir helfen? Vielen Dank im Voraus!

Antwort

0

Ich fand die Lösung.

Meine xhtml Datei sollte <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> und auch einen <title> Abschnitt enthalten.

Meine komplette xhtml Datei ist wie folgt zusammen:

<!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> 

ich drei zusätzliche jar Dateien hinzuzufügen hatte: (a) core-renderer.jar, (b) core-renderer-minimal.jar und (c) xml-apis-xerces-2.9.1.jar.

Es ist wichtig, alle diese Arbeiten nur mit iText-2.0.8.jar Datei zu erwähnen.

Auch ich musste import com.lowagie.text.DocumentException; und import org.xhtmlrenderer.pdf.ITextRenderer;

importieren
Verwandte Themen