Skip to content

Commit

Permalink
Merge pull request #485 from syjer/484-data-image-svg-xml-base64
Browse files Browse the repository at this point in the history
implement support for data:image/svg+xml;base64 in img src. Fixes #484
  • Loading branch information
danfickle committed May 16, 2020
2 parents e59bb36 + cd8b5e2 commit 7771b60
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html>
<head>
<style>
@page {
size: 30px 30px;
margin: 0;
padding:0;
}
</style>
</head>
<body>
<div><img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMTAiIHdpZHRoPSIxMCI+CiAgICA8Y2lyY2xlIGlkPSJpY29uLTEiIGN4PSI1IiBjeT0iNSIgcj0iNCIgc3Ryb2tlPSJibGFjayIgc3Ryb2tlLXdpZHRoPSIxIiBmaWxsPSJncmVlbiIgLz4KPC9zdmc+Cg=="/></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,14 @@ public void testCssFontFaceRuleGoogle() throws IOException {
assertTrue(vt.runTest("css-font-face-rule-google"));
}

/**
* Test that img with src="data:image/svg+xml;base64,...." work.
*/
@Test
public void testIssue484ImgSrcDataImageSvgBase64() throws IOException {
assertTrue(vt.runTest("issue-484-data-image-svg-xml-base64", TestSupport.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 @@ -22,10 +22,16 @@
import com.openhtmltopdf.extend.*;
import com.openhtmltopdf.layout.LayoutContext;
import com.openhtmltopdf.render.BlockBox;
import com.openhtmltopdf.resource.ImageResource;
import com.openhtmltopdf.resource.XMLResource;

import com.openhtmltopdf.util.ImageUtil;
import org.w3c.dom.Element;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.StringReader;

public class PdfBoxReplacedElementFactory implements ReplacedElementFactory {
private final SVGDrawer _svgImpl;
private final SVGDrawer _mathmlImpl;
Expand Down Expand Up @@ -56,10 +62,10 @@ public ReplacedElement createReplacedElement(LayoutContext c, BlockBox box,
} else if (nodeName.equals("img")) {
String srcAttr = e.getAttribute("src");
if (srcAttr != null && srcAttr.length() > 0) {

//handle the case of linked svg from img tag
if (srcAttr.endsWith(".svg") && _svgImpl != null) {
XMLResource xml = uac.getXMLResource(srcAttr);
boolean isDataImageSvg = false;
if (_svgImpl != null && (srcAttr.endsWith(".svg") || (isDataImageSvg = srcAttr.startsWith("data:image/svg+xml;base64,")))) {
XMLResource xml = isDataImageSvg ? XMLResource.load(new ByteArrayInputStream(ImageUtil.getEmbeddedBase64Image(srcAttr))) : uac.getXMLResource(srcAttr);

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

0 comments on commit 7771b60

Please sign in to comment.