Skip to content
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

How to provide resolver for SVG? com.openhtmltopdf.extend.FSUriResolver not being used by SVG #444

Closed
ieugen opened this issue Feb 12, 2020 · 3 comments

Comments

@ieugen
Copy link
Contributor

ieugen commented Feb 12, 2020

Hello,

I'm having some difficulties rendering PDF sprites. I followed the instructions in https://github.com/danfickle/openhtmltopdf/wiki/Plugins:-SVG-Images#svg-sprites .

I expected the SVG code to use the logic in FSUriResolver implementation but it does not and I don't know how to provide an implementation to fix the path.

I believe we should be able to change the base path for resolving SVG's in the filesystem / remote.
Any hints as to where/how can this be done?

Thanks,

More details bellow:

We use relative urls for the sprites and we get:

2020-02-12 22:19:21.670  WARN 520661 --- [ault-executor-0] com.openhtmltopdf.exception              : Couldn't draw SVG.

org.apache.batik.transcoder.TranscoderException: null
Enclosed Exception:
report-assets/sprites/solid.svg (No such file or directory)
	at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:226) ~[batik-transcoder-1.12.jar:na]
	at com.openhtmltopdf.svgsupport.PDFTranscoder.transcode(PDFTranscoder.java:216) ~[openhtmltopdf-svg-support-1.0.1.jar:na]
	at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142) ~[batik-transcoder-1.12.jar:na]
	at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156) ~[batik-transcoder-1.12.jar:na]
	at com.openhtmltopdf.svgsupport.BatikSVGImage.drawSVG(BatikSVGImage.java:185) ~[openhtmltopdf-svg-support-1.0.1.jar:na]
	at com.openhtmltopdf.pdfboxout.PdfBoxSVGReplacedElement.paint(PdfBoxSVGReplacedElement.java:70) [openhtmltopdf-pdfbox-1.0.1.jar:na]
	at com.openhtmltopdf.pdfboxout.PdfBoxFastOutputDevice.paintReplacedElement(PdfBoxFastOutputDevice.java:275) [openhtmltopdf-pdfbox-1.0.1.jar:na]
	at com.openhtmltopdf.render.displaylist.DisplayListPainter.paintReplacedElement(DisplayListPainter.java:167) [openhtmltopdf-core-1.0.1.jar:na]
	at com.openhtmltopdf.render.displaylist.DisplayListPainter.paintReplacedElements(DisplayListPainter.java:149) [openhtmltopdf-core-1.0.1.jar:na]
	at com.openhtmltopdf.render.displaylist.DisplayListPainter.paint(DisplayListPainter.java:266) [openhtmltopdf-core-1.0.1.jar:na]
	at com.openhtmltopdf.render.displaylist.DisplayListPainter.paintInlineContent(DisplayListPainter.java:129) [openhtmltopdf-core-1.0.1.jar:na]
	at com.openhtmltopdf.render.displaylist.DisplayListPainter.paint(DisplayListPainter.java:260) [openhtmltopdf-core-1.0.1.jar:na]
	at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.paintPageFast(PdfBoxRenderer.java:898) [openhtmltopdf-pdfbox-1.0.1.jar:na]
	at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.writePDFFast(PdfBoxRenderer.java:615) [openhtmltopdf-pdfbox-1.0.1.jar:na]
	at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPdfFast(PdfBoxRenderer.java:550) [openhtmltopdf-pdfbox-1.0.1.jar:na]
	at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:468) [openhtmltopdf-pdfbox-1.0.1.jar:na]
	at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:405) [openhtmltopdf-pdfbox-1.0.1.jar:na]
	at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:387) [openhtmltopdf-pdfbox-1.0.1.jar:na]
	at com.openhtmltopdf.pdfboxout.PdfRendererBuilder.run(PdfRendererBuilder.java:41) [openhtmltopdf-pdfbox-1.0.1.jar:na]
	at com.gr8pi.reportgen.openhtmltopdf.OpenHtmlToPdfRenderer.render(OpenHtmlToPdfRenderer.java:119) [reportgen-openhtmltopdf-5.1.0-SNAPSHOT.jar:na]
	at gr8pi.reportgen.renderer.grpc.DefaultSurveyReports.renderReport(DefaultSurveyReports.java:107) [reportgen-renderer-5.1.0-SNAPSHOT.jar:na]
	at gr8pi.reportgen.renderer.grpc.DefaultSurveyReports.generateComparativeReport(DefaultSurveyReports.java:50) [reportgen-renderer-5.1.0-SNAPSHOT.jar:na]
	at gr8pi.reportgen.grpc.SurveyReportsGrpc$MethodHandlers.invoke(SurveyReportsGrpc.java:229) [reportgen-grpc-api-5.1.0-SNAPSHOT.jar:na]
	at io.grpc.stub.ServerCalls$UnaryServerCallHandler$UnaryServerCallListener.onHalfClose(ServerCalls.java:171) [grpc-stub-1.20.0.jar:1.20.0]
	at io.grpc.internal.ServerCallImpl$ServerStreamListenerImpl.halfClosed(ServerCallImpl.java:322) [grpc-core-1.21.0.jar:1.21.0]
	at io.grpc.internal.ServerImpl$JumpToApplicationThreadServerStreamListener$1HalfClosed.runInContext(ServerImpl.java:762) [grpc-core-1.21.0.jar:1.21.0]
	at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37) [grpc-core-1.21.0.jar:1.21.0]
	at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123) [grpc-core-1.21.0.jar:1.21.0]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_242]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_242]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_242]
Caused by: org.apache.batik.bridge.BridgeException: report-assets/sprites/solid.svg (No such file or directory)
	at org.apache.batik.bridge.BridgeContext.getReferencedNode(BridgeContext.java:788) ~[batik-bridge-1.12.jar:na]
	at org.apache.batik.bridge.BridgeContext.getReferencedElement(BridgeContext.java:804) ~[batik-bridge-1.12.jar:na]
	at org.apache.batik.bridge.SVGUseElementBridge.buildCompositeGraphicsNode(SVGUseElementBridge.java:124) ~[batik-bridge-1.12.jar:na]
	at org.apache.batik.bridge.SVGUseElementBridge.createGraphicsNode(SVGUseElementBridge.java:98) ~[batik-bridge-1.12.jar:na]
	at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(GVTBuilder.java:213) ~[batik-bridge-1.12.jar:na]
	at org.apache.batik.bridge.GVTBuilder.buildComposite(GVTBuilder.java:171) ~[batik-bridge-1.12.jar:na]
	at org.apache.batik.bridge.GVTBuilder.build(GVTBuilder.java:82) ~[batik-bridge-1.12.jar:na]
	at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:208) ~[batik-transcoder-1.12.jar:na]
	... 30 common frames omitted


and ... 

Caused by: org.apache.batik.bridge.BridgeException: report-assets/sprites/solid.svg (No such file or directory)

The SVG iserting looks like (we use react, hence the attribute capitalization):

    <svg
      xmlns="http://www.w3.org/2000/svg"
      xmlnsXlink="http://www.w3.org/1999/xlink"
      width="18"
      height="18"
    >
      <use xlinkHref="report-assets/sprites/solid.svg" width="18" height="18" fill={color} />
    </svg>

Our code to render the PDF:

public PdfRendererBuilder pdfRenderer() throws IOException {

    XRLog.setLoggingEnabled(true);
    XRLog.setLoggerImpl(xrLogger);

    PdfRendererBuilder builder = new PdfRendererBuilder();
    builder.useFastMode();
    builder.useUnicodeBidiSplitter(bidiSplitterFactory());
    builder.useUnicodeBidiReorderer(bidiReorderer());
    builder.defaultTextDirection(BaseRendererBuilder.TextDirection.LTR); // OR RTL
    builder.useUriResolver(fsUriResolver);
    builder.useHttpStreamImplementation(fsStreamFactory);
    builder.useObjectDrawerFactory(new StandardObjectDrawerFactory());
    builder.useSVGDrawer(svgDrawer());
    builder.useMathMLDrawer(mathMLDrawer());
    builder.useCacheStore(PdfRendererBuilder.CacheStore.PDF_FONT_METRICS, fsCacheEx);

    if (registerFonts) {
      registerFonts(builder);
    }
    return builder;
  }
@ieugen
Copy link
Contributor Author

ieugen commented Feb 12, 2020

As a note: I can use http urls instead of relative urls for SVG's but this breaks our development/testing flow and it's not a good solution.

@ieugen
Copy link
Contributor Author

ieugen commented Feb 12, 2020

I found the note in the open issues https://github.com/danfickle/openhtmltopdf/wiki/Plugins:-SVG-Images#open-issues :( and added a link back to this issue.

@ieugen
Copy link
Contributor Author

ieugen commented Feb 12, 2020

I used a workaround by providing a symbolic link in the working directory to the assets.
I could not find a fix in the openhtmltopdf-svg module :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant