Skip to content

Commit

Permalink
#353 Makes sure a missing SVG linked from img tag wont blow up render.
Browse files Browse the repository at this point in the history
With test.
  • Loading branch information
danfickle committed Jun 28, 2019
1 parent f57a18d commit 551ee3d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
package com.openhtmltopdf.swing;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
Expand Down Expand Up @@ -328,22 +326,13 @@ public XMLResource getXMLResource(String uri) {
return null;
}

Reader inputReader = openReader(resolved);
XMLResource xmlResource;

try {
xmlResource = XMLResource.load(inputReader);
} finally {
if (inputReader != null) {
try {
inputReader.close();
} catch (IOException e) {
// swallow
}
}
try (Reader inputReader = openReader(resolved)) {
return inputReader == null ? null :
XMLResource.load(inputReader);
} catch (IOException e) {
// On auto close, swallow.
return null;
}

return xmlResource;
}

@Override
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<html>
<head>
<style>
@page {
size: 200px 200px;
margin: 0;
}
</style>
</head>
<body>
<img src="../../demos/images/circle.svg" alt="Circle" />

<img src="this-image-does-not-exist.svg" alt="Whoops!" />

<img src="/also-non-existant.svg" alt="Bad!" />
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,16 @@ public void testMaliciousSvgInsecureMode() throws IOException {
}));
}

/**
* Tests that a SVG image referenced from a <code>img</code> tag
* successfully renders. Also make sure a missing SVG resource
* does not shutdown rendering altogether. Issue 353.
*/
@Test
public void testSvgLinkedFromImgTag() throws IOException {
assertTrue(vt.runTest("svg-linked-from-img-tag", WITH_SVG));
}

// TODO:
// + Elements that appear just on generated overflow pages.
// + content property (page counters, etc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
import com.openhtmltopdf.extend.*;
import com.openhtmltopdf.layout.LayoutContext;
import com.openhtmltopdf.render.BlockBox;
import com.openhtmltopdf.resource.XMLResource;

import org.w3c.dom.Element;

public class PdfBoxReplacedElementFactory implements ReplacedElementFactory {
private final PdfBoxOutputDevice _outputDevice;
private final SVGDrawer _svgImpl;
private final SVGDrawer _mathmlImpl;
private final FSObjectDrawerFactory _objectDrawerFactory;

public PdfBoxReplacedElementFactory(PdfBoxOutputDevice outputDevice, SVGDrawer svgImpl, FSObjectDrawerFactory objectDrawerFactory, SVGDrawer mathmlImpl) {
_outputDevice = outputDevice;
_svgImpl = svgImpl;
_objectDrawerFactory = objectDrawerFactory;
_mathmlImpl = mathmlImpl;
Expand All @@ -59,7 +59,13 @@ public ReplacedElement createReplacedElement(LayoutContext c, BlockBox box,

//handle the case of linked svg from img tag
if (srcAttr.endsWith(".svg") && _svgImpl != null) {
return new PdfBoxSVGReplacedElement(uac.getXMLResource(srcAttr).getDocument().getDocumentElement(), _svgImpl, cssWidth, cssHeight, box, c, c.getSharedContext());
XMLResource xml = uac.getXMLResource(srcAttr);

if (xml != null) {
return new PdfBoxSVGReplacedElement(xml.getDocument().getDocumentElement(), _svgImpl, cssWidth, cssHeight, box, c, c.getSharedContext());
}

return null;
}

FSImage fsImage = uac.getImageResource(srcAttr).getImage();
Expand Down

0 comments on commit 551ee3d

Please sign in to comment.