Skip to content

Commit

Permalink
Added methods getting data from bytes array and returning resuls as t…
Browse files Browse the repository at this point in the history
…he bytes array
  • Loading branch information
aksndr committed Aug 24, 2017
1 parent b889902 commit 90b308a
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 12 deletions.
5 changes: 5 additions & 0 deletions flying-saucer-pdf-itext5/pom.xml
Expand Up @@ -32,6 +32,11 @@
</distributionManagement>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 90b308a

Please sign in to comment.