Skip to content

Commit

Permalink
#80 Example how to output different HTML files into the same PDF
Browse files Browse the repository at this point in the history
document.

(cherry picked from commit e7daf36)
  • Loading branch information
rototor committed May 22, 2018
1 parent 3a03faf commit 1319ded
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private static void renderPDF(String html, OutputStream outputStream) throws Exc
}
}

private static DefaultObjectDrawerFactory buildObjectDrawerFactory() {
static DefaultObjectDrawerFactory buildObjectDrawerFactory() {
DefaultObjectDrawerFactory objectDrawerFactory = new StandardObjectDrawerFactory();
objectDrawerFactory.registerDrawer("custom/binary-tree", new SampleObjectDrawerBinaryTree());
return objectDrawerFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<html xmlns="http://www.w3.org/1999/html">
<head>
<bookmarks>
<bookmark href="#werkstattAnschrift" name="In document Bookmark 1"/>
<bookmark href="#table_to_bookmark" name="table_to_bookmark"/>
<bookmark name="OpenHTMLToPDF">
<bookmark href="https://github.com/danfickle/openhtmltopdf" name="Github Homepage"/>
<bookmark href="https://github.com/danfickle/openhtmltopdf/issues/80" name="An random issue"/>
</bookmark>
</bookmarks>
<!--suppress CssUnknownProperty -->
<style>

Expand Down Expand Up @@ -280,7 +288,7 @@ <h1>Lorum ipsum .... XYZ........ZYX, SOMENUMBERS......</h1>
<br/>


<table class="framed_table framed_cells" cellspacing="0" cellpadding="0">
<table class="framed_table framed_cells" cellspacing="0" cellpadding="0" id="table_to_bookmark">
<tr class="table_repeat_header">
<td colspan="9">XYZABCDEFGZX</td>
</tr>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.openhtmltopdf.testcases;

import static com.openhtmltopdf.testcases.TestcaseRunner.buildObjectDrawerFactory;

import java.io.File;

import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.Charsets;
import org.junit.Test;

import com.openhtmltopdf.bidi.support.ICUBidiReorderer;
import com.openhtmltopdf.bidi.support.ICUBidiSplitter;
import com.openhtmltopdf.latexsupport.LaTeXDOMMutator;
import com.openhtmltopdf.mathmlsupport.MathMLDrawer;
import com.openhtmltopdf.outputdevice.helper.BaseRendererBuilder;
import com.openhtmltopdf.pdfboxout.PdfBoxRenderer;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import com.openhtmltopdf.svgsupport.BatikSVGDrawer;

public class ConcateOutputTest {

@Test
public void testConcateOutput() throws Exception {
File targetFile = new File("target/test/concatoutput/concated.pdf");
targetFile.getParentFile().mkdirs();
PDDocument doc = new PDDocument(MemoryUsageSetting.setupMixed(1000000));

for (String testCaseFile : new String[] { "color", "background-color",
"FSPageBreakMinHeightSample", "math-ml", "multi-column-layout", "simplerotate",
"svg-inline", "svg-sizes", "transform", "RepeatedTableSample",
"RepeatedTableTransformSample" }) {
renderPDF(testCaseFile, doc);
}

doc.save(targetFile);
doc.close();

}

private static void renderPDF(String testCaseFile, PDDocument document) throws Exception {
byte[] htmlBytes = IOUtils
.toByteArray(TestcaseRunner.class.getResourceAsStream("/testcases/" + testCaseFile + ".html"));
String html = new String(htmlBytes, Charsets.UTF_8);
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.useUnicodeBidiSplitter(new ICUBidiSplitter.ICUBidiSplitterFactory());
builder.useUnicodeBidiReorderer(new ICUBidiReorderer());
builder.defaultTextDirection(BaseRendererBuilder.TextDirection.LTR);
builder.useSVGDrawer(new BatikSVGDrawer());
builder.useMathMLDrawer(new MathMLDrawer());
builder.addDOMMutator(LaTeXDOMMutator.INSTANCE);
builder.useObjectDrawerFactory(buildObjectDrawerFactory());
builder.withHtmlContent(html, TestcaseRunner.class.getResource("/testcases/").toString());
builder.usePDDocument(document);
PdfBoxRenderer pdfBoxRenderer = builder.buildPdfRenderer();
pdfBoxRenderer.createPDFWithoutClosing();
}
}

0 comments on commit 1319ded

Please sign in to comment.