From 90b308aae28570c1e65f05c8e26d2711e691adf7 Mon Sep 17 00:00:00 2001 From: aksndr Date: Thu, 24 Aug 2017 16:07:09 +0300 Subject: [PATCH 1/3] Added methods getting data from bytes array and returning resuls as the bytes array --- flying-saucer-pdf-itext5/pom.xml | 5 + .../org/xhtmlrenderer/pdf/ITextRenderer.java | 15 ++- .../org/xhtmlrenderer/simple/PDFRenderer.java | 110 +++++++++++++++++- 3 files changed, 118 insertions(+), 12 deletions(-) diff --git a/flying-saucer-pdf-itext5/pom.xml b/flying-saucer-pdf-itext5/pom.xml index 3e7762471..a02a7a2d2 100644 --- a/flying-saucer-pdf-itext5/pom.xml +++ b/flying-saucer-pdf-itext5/pom.xml @@ -32,6 +32,11 @@ + + junit + junit + 4.12 + com.itextpdf itextpdf diff --git a/flying-saucer-pdf-itext5/src/main/java/org/xhtmlrenderer/pdf/ITextRenderer.java b/flying-saucer-pdf-itext5/src/main/java/org/xhtmlrenderer/pdf/ITextRenderer.java index 5cef8a144..3cf10e460 100644 --- a/flying-saucer-pdf-itext5/src/main/java/org/xhtmlrenderer/pdf/ITextRenderer.java +++ b/flying-saucer-pdf-itext5/src/main/java/org/xhtmlrenderer/pdf/ITextRenderer.java @@ -22,14 +22,7 @@ import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Shape; -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; -import java.io.StringReader; -import java.io.StringWriter; -import java.io.UnsupportedEncodingException; -import java.io.Writer; +import java.io.*; import java.util.List; import java.util.regex.Pattern; @@ -152,6 +145,12 @@ public void setDocument(File file) throws IOException { setDocument(loadDocument(file.toURI().toURL().toExternalForm()), (parent == null ? "" : parent.toURI().toURL().toExternalForm())); } + public void setDocument(byte[] bytes) throws IOException { + ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); + Document dom = XMLResource.load(inputStream).getDocument(); + setDocument(dom, ""); + } + public void setDocumentFromString(String content) { setDocumentFromString(content, null); } diff --git a/flying-saucer-pdf-itext5/src/main/java/org/xhtmlrenderer/simple/PDFRenderer.java b/flying-saucer-pdf-itext5/src/main/java/org/xhtmlrenderer/simple/PDFRenderer.java index e826e3d35..39a7969f7 100644 --- a/flying-saucer-pdf-itext5/src/main/java/org/xhtmlrenderer/simple/PDFRenderer.java +++ b/flying-saucer-pdf-itext5/src/main/java/org/xhtmlrenderer/simple/PDFRenderer.java @@ -4,10 +4,7 @@ import com.itextpdf.text.pdf.PdfWriter; import org.xhtmlrenderer.pdf.ITextRenderer; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; +import java.io.*; import java.util.Map; import java.util.HashMap; @@ -111,6 +108,80 @@ public static void renderToPDF(File file, String pdf, Character pdfVersion) doRenderToPDF(renderer, pdf); } + /** + * Renders the XML file as a PDF file and return its bytes. + * + * @param bytes XML file bytes to render + * @throws IOException if the file or PDF location is + * invalid + * @throws DocumentException if an error occurred + * while building the Document. + */ + public static byte[] renderToPDFBytes(byte[] bytes) + throws IOException, DocumentException { + + byte[] b = renderToPDFBytes(bytes, null); + return b; + } + + /** + * Renders the XML file as a PDF file return its bytes. + * + * @param bytes XML file bytes to render + * @param pdfVersion version of PDF to output; null uses default version + * @throws IOException if the file or PDF location is + * invalid + * @throws DocumentException if an error occurred + * while building the Document. + */ + public static byte[] renderToPDFBytes(byte[] bytes, Character pdfVersion) + throws IOException, DocumentException { + + ITextRenderer renderer = new ITextRenderer(); + renderer.setDocument(bytes); + if (pdfVersion != null) renderer.setPDFVersion(pdfVersion.charValue()); + byte[] b = doRenderToPDF(renderer); + return b; + } + + + /** + * Renders the XML file as a PDF file at the target location. + * + * @param bytes XML file bytes to render + * @param pdf path to the PDF file to create + * @throws IOException if the file or PDF location is + * invalid + * @throws DocumentException if an error occurred + * while building the Document. + */ + public static void renderToPDF(byte[] bytes, String pdf) + throws IOException, DocumentException { + + renderToPDF(bytes, pdf, null); + } + + /** + * Renders the XML file as a PDF file at the target location. + * + * @param bytes XML file bytes to render + * @param pdf path to the PDF file to create + * @param pdfVersion version of PDF to output; null uses default version + * @throws IOException if the file or PDF location is + * invalid + * @throws DocumentException if an error occurred + * while building the Document. + */ + public static void renderToPDF(byte[] bytes, String pdf, Character pdfVersion) + throws IOException, DocumentException { + + ITextRenderer renderer = new ITextRenderer(); + renderer.setDocument(bytes); + if (pdfVersion != null) renderer.setPDFVersion(pdfVersion.charValue()); + doRenderToPDF(renderer, pdf); + } + + /** * Internal use, runs the render process * @param renderer @@ -139,6 +210,37 @@ private static void doRenderToPDF(ITextRenderer renderer, String pdf) } } + /** + * Internal use, runs the render process + * @param renderer + * @throws com.itextpdf.text.DocumentException + * @throws java.io.IOException + */ + private static byte[] doRenderToPDF(ITextRenderer renderer) + throws IOException, DocumentException { + ByteArrayOutputStream os = null; + byte[] pdf; + try { + os = new ByteArrayOutputStream(); + renderer.layout(); + renderer.createPDF(os); + + pdf = os.toByteArray(); + + os.close(); + os = null; + } finally { + if (os != null) { + try { + os.close(); + } catch (IOException e) { + // ignore + } + } + } + return pdf; + } + /** * Renders a file or URL to a PDF. Command line use: first * argument is URL or file path, second From 56edd63e48f23304466f3a2e3d45e5e2fc519aa2 Mon Sep 17 00:00:00 2001 From: Alexandr Date: Thu, 24 Aug 2017 16:14:07 +0300 Subject: [PATCH 2/3] Update pom.xml --- flying-saucer-pdf-itext5/pom.xml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/flying-saucer-pdf-itext5/pom.xml b/flying-saucer-pdf-itext5/pom.xml index a02a7a2d2..23b2828b0 100644 --- a/flying-saucer-pdf-itext5/pom.xml +++ b/flying-saucer-pdf-itext5/pom.xml @@ -31,12 +31,7 @@ - - - junit - junit - 4.12 - + com.itextpdf itextpdf From 18787d144d5e468748bb2c4eae1393e5e484377f Mon Sep 17 00:00:00 2001 From: Alexandr Date: Thu, 24 Aug 2017 16:15:00 +0300 Subject: [PATCH 3/3] Update pom.xml --- flying-saucer-pdf-itext5/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flying-saucer-pdf-itext5/pom.xml b/flying-saucer-pdf-itext5/pom.xml index 23b2828b0..3e7762471 100644 --- a/flying-saucer-pdf-itext5/pom.xml +++ b/flying-saucer-pdf-itext5/pom.xml @@ -31,7 +31,7 @@ - + com.itextpdf itextpdf