Taras Melnyk
Taras Melnyk

Reputation: 3265

Svg image is not displayed on PDF file

I'm using 0.0.1-RC14 openhtmltopdf version for generating PDF files from html. I have svg image on my html template. This is a snippet of my code:

PdfRendererBuilder builder = new PdfRendererBuilder();
builder.defaultTextDirection(BaseRendererBuilder.TextDirection.LTR); 
builder.useSVGDrawer(new BatikSVGDrawer()); builder.useMathMLDrawer(new 
MathMLDrawer()); builder.addDOMMutator(LaTeXDOMMutator.INSTANCE); 
builder.useDefaultPageSize(210, 297, BaseRendererBuilder.PageSizeUnits.MM); 
builder.withHtmlContent(html, "");
builder.toStream(os); 
builder.run();

What I'm doing wrong or what I'm missing?

Upvotes: 1

Views: 4645

Answers (3)

Zach Siegel
Zach Siegel

Reputation: 1

Support for loading SVG images via the <img> tag was not supported by openhtmltopdf until this PR on July 16, 2018, which is referenced in the issue linked above.

Note that for the SVG renderer to be invoked, the URI must either end with the string "svg" or begin with the string "data:image/svg+xml;base64,".

Upvotes: 0

Kiran Duba
Kiran Duba

Reputation: 1

Im using openhtmlpdf-svg-support 1.0.7 version, Im not able to add SVG in PDF.

My code is something like this :

<!DOCTYPE HTML>
<html>
    <body>
        <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> 
            <image x="20" y="20" width="300" height="80" xlink:href="https://upload.wikimedia.org/wikipedia/commons/1/11/Test-Logo.svg"/>
        </svg>
    </body>
</html>
final PdfRendererBuilder builder = new PdfRendererBuilder();

builder.withHtmlContent(content, null);
builder.useSVGDrawer(new BatikSVGDrawer());

try {
    builder.toStream(
            new FileOutputStream("C:\\Codebase\\spring\\pdf\\src\\main\\java\\pdfbox\\FinalSVG.pdf"));
    builder.run();
} catch (final Exception e) {
    e.printStackTrace();
}

Upvotes: 0

Taras Melnyk
Taras Melnyk

Reputation: 3265

I've fixed the issue after upgrading openhtmltopdf library to the latest version: 0.0.1-RC15 and adding namespace attribute to the svg element: xmlns="http://www.w3.org/2000/svg" :

<svg xmlns="http://www.w3.org/2000/svg" ...> ... </svg>"

For detailed information refer to https://github.com/danfickle/openhtmltopdf/wiki/Plugins:-SVG-Images or https://github.com/danfickle/openhtmltopdf/issues/258#issuecomment-410269938

Upvotes: 1

Related Questions