vsch / flexmark-java

CommonMark/Markdown Java parser with source level AST. CommonMark 0.28, emulation of: pegdown, kramdown, markdown.pl, MultiMarkdown. With HTML to MD, MD to PDF, MD to DOCX conversion modules.
BSD 2-Clause "Simplified" License
2.3k stars 272 forks source link

Set default SVGDrawer in PdfConverterExtension.exportToPdf or expose some option to do it manually #337

Open xnike opened 5 years ago

xnike commented 5 years ago

Hi, SVG image is not supported by PdfConverterExtension

Unrecognized image format for: file:./docs/some.svg
Can't read image file; unexpected problem for URI 'file:./docs/src/rbm.kotlinmeetup/poc.architecture.svg'
java.io.IOException: Unrecognized Image format
        at com.openhtmltopdf.pdfboxout.PdfBoxImage.<init>(PdfBoxImage.java:54)
        at com.openhtmltopdf.pdfboxout.PdfBoxUserAgent.getImageResource(PdfBoxUserAgent.java:99)
        at com.openhtmltopdf.pdfboxout.PdfBoxReplacedElementFactory.createReplacedElement(PdfBoxReplacedElementFactory.java:65)
        at com.openhtmltopdf.render.BlockBox.calcDimensions(BlockBox.java:767)
        at com.openhtmltopdf.render.BlockBox.calcDimensions(BlockBox.java:707)
        at com.openhtmltopdf.render.BlockBox.collapseTopMargin(BlockBox.java:1257)
        at com.openhtmltopdf.render.BlockBox.collapseTopMargin(BlockBox.java:1274)
        at com.openhtmltopdf.render.BlockBox.collapseMargins(BlockBox.java:1207)
        at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:875)
        at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:827)
        at com.openhtmltopdf.layout.BlockBoxing.layoutBlockChild0(BlockBoxing.java:321)
        at com.openhtmltopdf.layout.BlockBoxing.layoutBlockChild(BlockBoxing.java:299)
        at com.openhtmltopdf.layout.BlockBoxing.layoutContent(BlockBoxing.java:90)
        at com.openhtmltopdf.render.BlockBox.layoutChildren(BlockBox.java:1057)
        at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:911)
        at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:827)
        at com.openhtmltopdf.layout.BlockBoxing.layoutBlockChild0(BlockBoxing.java:321)
        at com.openhtmltopdf.layout.BlockBoxing.layoutBlockChild(BlockBoxing.java:299)
        at com.openhtmltopdf.layout.BlockBoxing.layoutContent(BlockBoxing.java:90)
        at com.openhtmltopdf.render.BlockBox.layoutChildren(BlockBox.java:1057)
        at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:911)
        at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:827)
        at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.layout(PdfBoxRenderer.java:335)
        at com.vladsch.flexmark.pdf.converter.PdfConverterExtension.exportToPdf(PdfConverterExtension.java:130)
        at com.vladsch.flexmark.pdf.converter.PdfConverterExtension.exportToPdf(PdfConverterExtension.java:100)
        at com.vladsch.flexmark.pdf.converter.PdfConverterExtension.exportToPdf(PdfConverterExtension.java:90)

As I see openhtmltopdf supports setting SVGDrawer and rendering SVG files. It'll be great if PdfConverterExtension will have this option enabled by default or somehow configurable.

vsch commented 5 years ago

@xnike, the way to achieve this is to use apache batik library to convert SVG to PNG and replace the svg links to the generated PNG image.

This should be done in a custom link resolver for image links to SVG files and replace them to the location of the generated and cached PNG file.

Sample custom link resolver can be used as a starting point.

CustomLinkResolverSample.java

I will consider adding an extension to handle this after Java 7 support is dropped from the library.