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

implement support for data:image/svg+xml;base64 in img src. See issue #484 #485

Merged
merged 1 commit into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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