-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #486 from syjer/444-batik-use-useragentcallback
svg backend: use openhtmltopdf UserAgentCallback for fetching external resources, fixes #444
- Loading branch information
Showing
15 changed files
with
216 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+1.33 KB
openhtmltopdf-examples/src/main/resources/visualtest/expected/svg-custom-protocol.pdf
Binary file not shown.
Binary file added
BIN
+784 Bytes
...ltopdf-examples/src/main/resources/visualtest/expected/svg-external-file-load-blocked.pdf
Binary file not shown.
Binary file added
BIN
+1.33 KB
...ples/src/main/resources/visualtest/expected/svg-external-file-whitelist-file-protocol.pdf
Binary file not shown.
3 changes: 3 additions & 0 deletions
3
openhtmltopdf-examples/src/main/resources/visualtest/html/solid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions
19
openhtmltopdf-examples/src/main/resources/visualtest/html/svg-custom-protocol.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<html> | ||
<head> | ||
<style> | ||
@page { | ||
size: 40px 40px; | ||
margin: 0; | ||
padding:0; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<svg xmlns="http://www.w3.org/2000/svg" | ||
xmlns:xlink="http://www.w3.org/1999/xlink" | ||
width="18" | ||
height="18"> | ||
<use xlink:href="custom:/solid.svg#icon-1" width="18" height="18" fill="red" /> | ||
</svg> | ||
</body> | ||
</html> |
19 changes: 19 additions & 0 deletions
19
...htmltopdf-examples/src/main/resources/visualtest/html/svg-external-file-load-blocked.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<html> | ||
<head> | ||
<style> | ||
@page { | ||
size: 40px 40px; | ||
margin: 0; | ||
padding:0; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<svg xmlns="http://www.w3.org/2000/svg" | ||
xmlns:xlink="http://www.w3.org/1999/xlink" | ||
width="18" | ||
height="18"> | ||
<use xlink:href="solid.svg#icon-1" width="18" height="18" fill="red" /> | ||
</svg> | ||
</body> | ||
</html> |
19 changes: 19 additions & 0 deletions
19
...xamples/src/main/resources/visualtest/html/svg-external-file-whitelist-file-protocol.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<html> | ||
<head> | ||
<style> | ||
@page { | ||
size: 40px 40px; | ||
margin: 0; | ||
padding:0; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<svg xmlns="http://www.w3.org/2000/svg" | ||
xmlns:xlink="http://www.w3.org/1999/xlink" | ||
width="18" | ||
height="18"> | ||
<use xlink:href="solid.svg#icon-1" width="18" height="18" fill="red" /> | ||
</svg> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...ltopdf-svg-support/src/main/java/com/openhtmltopdf/svgsupport/OpenHtmlDocumentLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.openhtmltopdf.svgsupport; | ||
|
||
import com.openhtmltopdf.extend.UserAgentCallback; | ||
import com.openhtmltopdf.util.XRLog; | ||
import org.apache.batik.bridge.DocumentLoader; | ||
import org.apache.batik.bridge.UserAgent; | ||
import org.w3c.dom.Document; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
public class OpenHtmlDocumentLoader extends DocumentLoader { | ||
|
||
private final UserAgentCallback userAgentCallback; | ||
|
||
public OpenHtmlDocumentLoader(UserAgent userAgent, UserAgentCallback userAgentCallback) { | ||
super(userAgent); | ||
this.userAgentCallback = userAgentCallback; | ||
} | ||
|
||
|
||
@Override | ||
public Document loadDocument(String uri) throws IOException { | ||
try { | ||
// special handling of relative uri in case of file protocol, we receive something like "file:file.svg" | ||
// The path will be null, but the scheme specific part will be not null | ||
URI parsedURI = new URI(uri); | ||
if ("file".equals(parsedURI.getScheme()) && parsedURI.getPath() == null && parsedURI.getSchemeSpecificPart() != null) { | ||
uri = userAgentCallback.resolveURI(parsedURI.getSchemeSpecificPart()); | ||
} else if (!parsedURI.isAbsolute()) { | ||
uri = userAgentCallback.resolveURI(uri); | ||
} | ||
} catch (URISyntaxException uriSyntaxException) { | ||
XRLog.exception("URI syntax exception while loading external svg resource: " + uri, uriSyntaxException); | ||
} | ||
return super.loadDocument(uri, new ByteArrayInputStream(userAgentCallback.getBinaryResource(uri))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.