Skip to content

Commit 7771b60

Browse files
authored
Merge pull request #485 from syjer/484-data-image-svg-xml-base64
implement support for data:image/svg+xml;base64 in img src. Fixes #484
2 parents e59bb36 + cd8b5e2 commit 7771b60

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
<head>
3+
<style>
4+
@page {
5+
size: 30px 30px;
6+
margin: 0;
7+
padding:0;
8+
}
9+
</style>
10+
</head>
11+
<body>
12+
<div><img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMTAiIHdpZHRoPSIxMCI+CiAgICA8Y2lyY2xlIGlkPSJpY29uLTEiIGN4PSI1IiBjeT0iNSIgcj0iNCIgc3Ryb2tlPSJibGFjayIgc3Ryb2tlLXdpZHRoPSIxIiBmaWxsPSJncmVlbiIgLz4KPC9zdmc+Cg=="/></div>
13+
</body>
14+
</html>

openhtmltopdf-examples/src/test/java/com/openhtmltopdf/visualregressiontests/VisualRegressionTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,14 @@ public void testCssFontFaceRuleGoogle() throws IOException {
11101110
assertTrue(vt.runTest("css-font-face-rule-google"));
11111111
}
11121112

1113+
/**
1114+
* Test that img with src="data:image/svg+xml;base64,...." work.
1115+
*/
1116+
@Test
1117+
public void testIssue484ImgSrcDataImageSvgBase64() throws IOException {
1118+
assertTrue(vt.runTest("issue-484-data-image-svg-xml-base64", TestSupport.WITH_SVG));
1119+
}
1120+
11131121
// TODO:
11141122
// + Elements that appear just on generated overflow pages.
11151123
// + content property (page counters, etc)

openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxReplacedElementFactory.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@
2222
import com.openhtmltopdf.extend.*;
2323
import com.openhtmltopdf.layout.LayoutContext;
2424
import com.openhtmltopdf.render.BlockBox;
25+
import com.openhtmltopdf.resource.ImageResource;
2526
import com.openhtmltopdf.resource.XMLResource;
2627

28+
import com.openhtmltopdf.util.ImageUtil;
2729
import org.w3c.dom.Element;
2830

31+
import java.io.ByteArrayInputStream;
32+
import java.io.DataInputStream;
33+
import java.io.StringReader;
34+
2935
public class PdfBoxReplacedElementFactory implements ReplacedElementFactory {
3036
private final SVGDrawer _svgImpl;
3137
private final SVGDrawer _mathmlImpl;
@@ -56,10 +62,10 @@ public ReplacedElement createReplacedElement(LayoutContext c, BlockBox box,
5662
} else if (nodeName.equals("img")) {
5763
String srcAttr = e.getAttribute("src");
5864
if (srcAttr != null && srcAttr.length() > 0) {
59-
6065
//handle the case of linked svg from img tag
61-
if (srcAttr.endsWith(".svg") && _svgImpl != null) {
62-
XMLResource xml = uac.getXMLResource(srcAttr);
66+
boolean isDataImageSvg = false;
67+
if (_svgImpl != null && (srcAttr.endsWith(".svg") || (isDataImageSvg = srcAttr.startsWith("data:image/svg+xml;base64,")))) {
68+
XMLResource xml = isDataImageSvg ? XMLResource.load(new ByteArrayInputStream(ImageUtil.getEmbeddedBase64Image(srcAttr))) : uac.getXMLResource(srcAttr);
6369

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

0 commit comments

Comments
 (0)