I've been working on converting HTML to PDF. Initially, I transformed my HTML code into XHTML using the resources provided in this link:
After successfully displaying the generated XHTML code in a browser by creating an HTML file for testing purposes, I proceeded to convert the HTML file to PDF using Java code.
public static final String DEST = "C:/Users/Turgut/Desktop/test12.pdf";
public static final String[] HTML = { "C:/Users/Turgut/Desktop/New Text Document (5).html", "C:/Users/Turgut/Desktop/New Text Document (5).html" };
public static void main(String[] args) {
File file = new File(DEST);
file.getParentFile().mkdirs();
new TestHtmlToPdf().createPdf(DEST);
}
public void createPdf(String file) {
Document document = new Document();
try {
//String HTML = "C:/Users/Turgut/Desktop/test12.html";
PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
String css = readCSS();
for (String htmlfile : HTML) {
String html = Utilities.readFileToString(htmlfile);
ElementList list = XMLWorkerHelper.parseToElementList(html, css);
for (Element e : list) {
document.add(e);
}
document.newPage();
}
document.close();
}
catch(IOException e) {
e.printStackTrace();
}
catch(DocumentException ex) {
ex.printStackTrace();
}
}
private String readCSS() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
InputStream is = XMLWorkerHelper.class.getResourceAsStream("/default.css");
while ((length = is.read(buffer)) != -1) {
baos.write(buffer, 0, length);
}
return new String(baos.toByteArray());
}
Included below is a style tag within the head tag:
<style type="text/css">
body {
background-color: #FFFFFF;
font-family: 'Tahoma', "Times New Roman", Times, serif;
font-size: 11px;
color: #666666;
}
h1, h2 {
padding-bottom: 3px;
padding-top: 3px;
margin-bottom: 5px;
color : #000000;
text-transform: uppercase;
font-family: Arial, Helvetica, sans-serif;
}
...
(Additional CSS styles)
...
td {
border-color:#000000;
}</style>
While I was able to generate the PDF file from the HTML file, it seems that there may be issues with reading the CSS block since there are discrepancies in text colors between the PDF and HTML files.
If anyone has suggestions on how to ensure CSS compatibility in generating the PDF file, I would greatly appreciate it. Thank you!