Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/aksndr/flyingsaucer into …
Browse files Browse the repository at this point in the history
…aksndr-master
  • Loading branch information
pbrant committed Sep 10, 2017
2 parents 25805ee + 18787d1 commit 60a028d
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 12 deletions.
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 60a028d

Please sign in to comment.