Skip to content

Commit

Permalink
Merge branch 'aksndr-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrant committed Sep 10, 2017
2 parents 25805ee + 3370dd7 commit 316dc7e
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
Expand Up @@ -23,6 +23,7 @@
import java.awt.Rectangle;
import java.awt.Shape;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -152,6 +153,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,6 +4,7 @@
import com.itextpdf.text.pdf.PdfWriter;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -111,6 +112,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 +214,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 316dc7e

Please sign in to comment.