-
Notifications
You must be signed in to change notification settings - Fork 359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a way to output an image file rather than a pdf? #73
Comments
Hi @Jo3McCarthy There is a Java2D output device in the core. See the swing package I think. Unfortunately, it is broken at the moment. Also see #63 Regards, |
Just saw this issue. With #74 I started to implement something similar to the PdfRendererBuilder. If you just want to quick render some HTML as Image you could use Graphics2DRenderer.renderToImageAutoSize(). But you need a URL (e.g. file.toURL().toExternalURI()) for that to work. With the Java2DRendererBuilder (name can still change) you can also render in memory HTML string as image. In the midterm I think we should get rid of the old Graphics2DRenderer and Java2DRenderer. The later does not even work at the moment. |
…r paged output. For now, I’ve copied accross the files I needed to alter to the java2d sub-module to avoid breaking things. Eventually we can get rid of all Java2D and Swing code from the core. Currently the renderer and builder in the sub-module are for paged output only but I think we can combine both paged and continuous in the one builder/renderer.
OK, I've implemented a new renderer and worked on the builder by @rototor (in a copy) to output to paged images. One can incorporate the java2d sub-module and use it thus (but it is likely to change): BufferedImage bf = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR);
final Graphics2D g2d = bf.createGraphics();
Java2DRendererBuilder builder = new Java2DRendererBuilder();
builder.withUri("file:///Users/me/Documents/pdftests/image-output-device-test.htm");
builder.useLayoutGraphics(g2d);
builder.usePageProcessor(new DefaultPageProcessor("/Users/me/Documents/pdftests/images/%page-number%.png"));
builder.useSVGDrawer(new BatikSVGDrawer());
Java2DRenderer renderer = builder.buildJava2DRenderer();
renderer.layout();
renderer.writePages(0);
g2d.dispose();
which will output a PNG image for each page of the document in the correct size. |
Start of a checklist for the Java2D (or any new) output device:
|
… (Still unstable and mostly unimplemented) [ci skip]
Thank you all for the input. Your suggestions have led to a partial solution, but I have mutually exclusive requirements at the moment. I can use the Graphics2DRenderer.renderToImageAutoSize() to render some of an image, but as I mentioned in Issue #71 the content I receive has HTML Entities from HTML5 (like " " and such). So, although I can use jsoup to parse the URL to a Document, I can not then feed that Document into Graphics2DRenderer.renderToImageAutoSize() because it requires a URL. By hand-tweaking the content before rendering, I get the full image at the requested size. But, hand-tweaking will not suffice for the use case. If anybody has already encountered this and has a solution, please share. Otherwise, I may have to go into the Graphics2DRenderer and implement another method that supports my requirement -- in which case, pray for me... |
RC10 has been released with image output in alpha state.
The builder code can be found here. You'll also need to include the Java2D submodule: <dependency>
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-java2d</artifactId>
<version>${openhtml.version}</version>
</dependency> Here is a simple example: DefaultPageProcessor processor = new DefaultPageProcessor(new FSPageOutputStreamSupplier() {
@Override
public OutputStream supply(int zeroBasedPageNumber) throws IOException {
return new FileOutputStream("/Users/you/Documents/pdftests/images/%page-number%.png".replace("%page-number%", String.valueOf(zeroBasedPageNumber)));
}
}, BufferedImage.TYPE_3BYTE_BGR, "png");
Graphics2D g2d = processor.createLayoutGraphics();
Java2DRendererBuilder builder = new Java2DRendererBuilder();
builder.withUri("file:///Users/you/Documents/pdftests/showcase.htm");
builder.useLayoutGraphics(g2d);
builder.useSVGDrawer(new BatikSVGDrawer()); // Optional
builder.toPageProcessor(processor);
builder.runPaged();
g2d.dispose(); |
I think this is the correct place to note that the layout for continuous image output mode is using "print" rather than "screen" media. This should be set by the builder, probably on the |
hi, @danfickle. the created image has a very low resolution / is very pixelated. is there a way to up the quality? thanks! File file = new File("test");
DefaultPageProcessor processor = new DefaultPageProcessor(new FSPageOutputStreamSupplier() {
@Override
public OutputStream supply(int zeroBasedPageNumber) throws IOException {
return new FileOutputStream(file);
}
}, BufferedImage.TYPE_3BYTE_BGR, "png");
Graphics2D g2d = processor.createLayoutGraphics();
Java2DRendererBuilder java2DRendererBuilder = new Java2DRendererBuilder();
java2DRendererBuilder.withHtmlContent(filledOutTemplate, PDFUtil.class.getResource("/fonts/").toExternalForm() + "../");;
java2DRendererBuilder.useEnvironmentFonts(true);
java2DRendererBuilder.useLayoutGraphics(g2d);
java2DRendererBuilder.useFastMode();
java2DRendererBuilder.buildJava2DRenderer();
java2DRendererBuilder.toPageProcessor(processor);
java2DRendererBuilder.runFirstPage();
g2d.dispose(); |
Hi, All;
I think the process of rendering has some interim processing steps where content is converted into images. Is there a method to output the images directly? Or, could you please point me to a point in the code where I could implement that function?
Thanks,
Jo3
The text was updated successfully, but these errors were encountered: