Html2pdf using open pdf (#11178)

* Added inital changes html to pdf using openPdf

* Added changes to pdf for external style

* Added chnages via flying saucer

* Added changes ti fix the issue

* Added inital changes

* Simplyfied the core logic

* Created a sperate package

* Added changes for making it more modular

* Added Image attribute update

* Added chnages to add images

* Added changes

* Style validation

* Added changes to fix the styles

* Review comments updates

* Corrected the versions

* Fixed the review commit

* Added changes to fix the indendation issue

Co-authored-by: Amitabh.Tiwari <amitabh.tiwari@maersk.com>
This commit is contained in:
Amitabh Tiwari 2021-09-17 07:31:41 +05:30 committed by GitHub
parent b441c62f0e
commit 2a68c0e2bb
7 changed files with 220 additions and 0 deletions

View File

@ -71,6 +71,26 @@
<artifactId>flying-saucer-pdf</artifactId>
<version>${flying-saucer-pdf.version}</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-openpdf</artifactId>
<version>${flying-saucer-pdf-openpdf.version}</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>${jsoup.version}</version>
</dependency>
<dependency>
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-core</artifactId>
<version>${open-html-pdf-core.version}</version>
</dependency>
<dependency>
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-pdfbox</artifactId>
<version>${open-html-pdfbox.version}</version>
</dependency>
</dependencies>
<build>
@ -93,6 +113,10 @@
<poi-ooxml.version>3.15</poi-ooxml.version>
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<flying-saucer-pdf.version>9.1.20</flying-saucer-pdf.version>
<open-html-pdfbox.version>1.0.6</open-html-pdfbox.version>
<open-html-pdf-core.version>1.0.6</open-html-pdf-core.version>
<flying-saucer-pdf-openpdf.version>9.1.22</flying-saucer-pdf-openpdf.version>
<jsoup.version>1.14.2</jsoup.version>
</properties>
</project>

View File

@ -0,0 +1,56 @@
package com.baeldung.pdf.openpdf;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.w3c.dom.Element;
import org.xhtmlrenderer.extend.FSImage;
import org.xhtmlrenderer.extend.ReplacedElement;
import org.xhtmlrenderer.extend.ReplacedElementFactory;
import org.xhtmlrenderer.extend.UserAgentCallback;
import org.xhtmlrenderer.layout.LayoutContext;
import org.xhtmlrenderer.pdf.ITextFSImage;
import org.xhtmlrenderer.pdf.ITextImageElement;
import org.xhtmlrenderer.render.BlockBox;
import org.xhtmlrenderer.simple.extend.FormSubmissionListener;
import com.lowagie.text.Image;
public class CustomElementFactoryImpl implements ReplacedElementFactory {
@Override
public ReplacedElement createReplacedElement(LayoutContext lc, BlockBox box, UserAgentCallback uac, int cssWidth, int cssHeight) {
Element e = box.getElement();
String nodeName = e.getNodeName();
if (nodeName.equals("img")) {
String imagePath = e.getAttribute("src");
try {
InputStream input = new FileInputStream("src/main/resources/" + imagePath);
byte[] bytes = IOUtils.toByteArray(input);
Image image = Image.getInstance(bytes);
FSImage fsImage = new ITextFSImage(image);
if (cssWidth != -1 || cssHeight != -1) {
fsImage.scale(cssWidth, cssHeight);
} else {
fsImage.scale(2000, 1000);
}
return new ITextImageElement(fsImage);
} catch (Exception e1) {
e1.printStackTrace();
}
}
return null;
}
@Override
public void reset() {
}
@Override
public void remove(Element e) {
}
@Override
public void setFormSubmissionListener(FormSubmissionListener listener) {
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.pdf.openpdf;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.xhtmlrenderer.layout.SharedContext;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Html2PdfUsingFlyingSaucer {
private static final String HTML_INPUT = "src/main/resources/htmlforopenpdf.html";
private static final String PDF_OUTPUT = "src/main/resources/html2pdf.pdf";
public static void main(String[] args) {
try {
Html2PdfUsingFlyingSaucer htmlToPdf = new Html2PdfUsingFlyingSaucer();
htmlToPdf.generateHtmlToPdf();
} catch (Exception e) {
e.printStackTrace();
}
}
private void generateHtmlToPdf() throws Exception {
File inputHTML = new File(HTML_INPUT);
Document inputHtml = createWellFormedHtml(inputHTML);
File outputPdf = new File(PDF_OUTPUT);
xhtmlToPdf(inputHtml, outputPdf);
}
private Document createWellFormedHtml(File inputHTML) throws IOException {
Document document = Jsoup.parse(inputHTML, "UTF-8");
document.outputSettings()
.syntax(Document.OutputSettings.Syntax.xml);
return document;
}
private void xhtmlToPdf(Document xhtml, File outputPdf) throws Exception {
try (OutputStream outputStream = new FileOutputStream(outputPdf)) {
ITextRenderer renderer = new ITextRenderer();
SharedContext sharedContext = renderer.getSharedContext();
sharedContext.setPrint(true);
sharedContext.setInteractive(false);
sharedContext.setReplacedElementFactory(new CustomElementFactoryImpl());
renderer.setDocumentFromString(xhtml.html());
renderer.layout();
renderer.createPDF(outputStream);
}
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.pdf.openpdf;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.FileSystems;
import org.jsoup.Jsoup;
import org.jsoup.helper.W3CDom;
import org.jsoup.nodes.Document;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
public class Html2PdfUsingOpenHtml {
private static final String HTML_INPUT = "src/main/resources/htmlforopenpdf.html";
private static final String PDF_OUTPUT = "src/main/resources/html2pdf.pdf";
public static void main(String[] args) {
try {
Html2PdfUsingOpenHtml htmlToPdf = new Html2PdfUsingOpenHtml();
htmlToPdf.generateHtmlToPdf();
} catch (Exception e) {
e.printStackTrace();
}
}
private void generateHtmlToPdf() throws IOException {
File inputHTML = new File(HTML_INPUT);
Document doc = createWellFormedHtml(inputHTML);
xhtmlToPdf(doc, PDF_OUTPUT);
}
private Document createWellFormedHtml(File inputHTML) throws IOException {
Document document = Jsoup.parse(inputHTML, "UTF-8");
document.outputSettings()
.syntax(Document.OutputSettings.Syntax.xml);
return document;
}
private void xhtmlToPdf(Document doc, String outputPdf) throws IOException {
try (OutputStream os = new FileOutputStream(outputPdf)) {
String baseUri = FileSystems.getDefault()
.getPath("src/main/resources/")
.toUri()
.toString();
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.withUri(outputPdf);
builder.toStream(os);
builder.withW3cDocument(new W3CDom().fromJsoup(doc), baseUri);
builder.run();
}
}
}

Binary file not shown.

View File

@ -0,0 +1,26 @@
<html>
<head>
<style>
.center_div {
border: 1px solid gray;
margin-left: auto;
margin-right: auto;
width: 90%;
background-color: #d0f0f6;
text-align: left;
padding: 8px;
}
</style>
<link href="style.css" rel="stylesheet">
</head>
<body>
<div class="center_div">
<h1>Hello Baeldung!</h1>
<img src="Java_logo.png">
<div class="myclass">
<p>This is the tutorial to convert html to pdf.</p>
</div>
</div>
</body>
</html>

View File

@ -0,0 +1,6 @@
.myclass{
font-family: Helvetica, sans-serif;
font-size:25;
font-weight: normal;
color: blue;
}