Skip to content

Commit

Permalink
Drop simple.Java2DRendererBuilder and implement single paged output
Browse files Browse the repository at this point in the history
Drop the Java2DRendererBuilder in the core package and use the java2d
package. Also provide a BufferedImage PageProcessor.

Allow to output everything as single page. And adapt the testdriver to
use both modes.
  • Loading branch information
rototor committed Mar 12, 2017
1 parent 92f2d8e commit ddcc72f
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 358 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.openhtmltopdf.css.parser.FSRGBColor;
import com.openhtmltopdf.extend.*;
import com.openhtmltopdf.render.*;
import com.openhtmltopdf.simple.Java2DRendererBuilder;

import javax.swing.*;
import java.awt.*;
Expand Down Expand Up @@ -181,10 +180,6 @@ public void paintReplacedElement(RenderingContext c, BlockBox box) {
Point location = replaced.getLocation();
_graphics.drawImage(
image, (int)location.getX(), (int)location.getY(), null);
} else if (replaced instanceof Java2DRendererBuilder.Graphics2DPaintingReplacedElement) {
Rectangle contentBounds = box.getContentAreaEdge(box.getAbsX(), box.getAbsY(), c);
((Java2DRendererBuilder.Graphics2DPaintingReplacedElement) replaced).paint(this, c, contentBounds.x,
contentBounds.y, contentBounds.width, contentBounds.height);
}
}

Expand Down
5 changes: 5 additions & 0 deletions openhtmltopdf-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
<artifactId>openhtmltopdf-svg-support</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-java2d</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import com.openhtmltopdf.bidi.support.ICUBidiReorderer;
import com.openhtmltopdf.bidi.support.ICUBidiSplitter;
import com.openhtmltopdf.java2d.api.BufferedImagePageProcessor;
import com.openhtmltopdf.java2d.api.DefaultPageProcessor;
import com.openhtmltopdf.java2d.api.FSPageOutputStreamSupplier;
import com.openhtmltopdf.java2d.api.Java2DRendererBuilder;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder.TextDirection;
import com.openhtmltopdf.simple.Java2DRendererBuilder;
import com.openhtmltopdf.svgsupport.BatikSVGDrawer;
import com.openhtmltopdf.util.JDKXRLogger;
import com.openhtmltopdf.util.XRLog;
Expand Down Expand Up @@ -144,13 +147,33 @@ private static void renderPDF(String html, OutputStream outputStream) throws Exc
}
}

private static void renderPNG(String html, OutputStream outputStream) throws IOException {
private static void renderPNG(String html, final String filename) throws Exception {
Java2DRendererBuilder builder = new Java2DRendererBuilder();
builder.useSVGDrawer(new BatikSVGDrawer());
builder.withHtmlContent(html, TestcaseRunner.class.getResource("/testcases/").toString());
BufferedImage image = builder.build().renderToImage(512, BufferedImage.TYPE_3BYTE_BGR);
ImageIO.write(image, "PNG", outputStream);
outputStream.close();
BufferedImagePageProcessor bufferedImagePageProcessor = new BufferedImagePageProcessor(
BufferedImage.TYPE_INT_RGB, 2.0);
builder.useDefaultPageSize(210, 297, Java2DRendererBuilder.PageSizeUnits.MM);

/*
* Render Single Page Image
*/
builder.toSinglePage(bufferedImagePageProcessor).runFirstPage();
BufferedImage image = bufferedImagePageProcessor.getPageImages().get(0);

FileOutputStream output = new FileOutputStream(filename);
ImageIO.write(image, "PNG", output);
output.close();

/*
* Render Multipage Image Files
*/
builder.toPageProcessor(new DefaultPageProcessor(new FSPageOutputStreamSupplier() {
@Override
public OutputStream supply(int zeroBasedPageNumber) throws IOException {
return new FileOutputStream(filename.replace(".png", "_" + zeroBasedPageNumber + ".png"));
}
}, BufferedImage.TYPE_INT_ARGB, "PNG")).runPaged();

}

Expand All @@ -165,7 +188,6 @@ public static void runTestCase(String testCaseFile) throws Exception {
renderPDF(html, outputStream);
System.out.println("Wrote " + testCaseOutputFile);

FileOutputStream outputStreamPNG = new FileOutputStream(testCaseOutputPNGFile);
renderPNG(html, outputStreamPNG);
renderPNG(html, testCaseOutputPNGFile);
}
}
Loading

0 comments on commit ddcc72f

Please sign in to comment.