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
Embedded SVG in HTML Capability #320
Comments
From marclaporte on January 06, 2012 01:09:54 |
From fabien.menager on January 12, 2012 20:13:43 |
From ger...@bourdin.name on September 28, 2012 10:22:10 |
From mar...@mgscreativa.com on September 28, 2012 14:06:53 |
I have fixed this issue in my local. If there is anyone who needs this patch, please let me know |
@oliverhuynh how have you gone about SVG support? We would be interested in knowing the details. |
I'd be interested too. The easiest way to share your implementation would be for you to fork this repository and push your changes on it. |
@oliverhuynh I totally agree, if you need help pushing to a Git clone, please tell us. |
Hi guys, DOMPDF can use PDFlib to build the PDF. My patch is updating the code with latest PDFLIB(9), adding SVG capacibility and so, the output PDF can embed SVG. I can attach the patch here. |
That's not going to be universal enough for the majority of users who, I suspect, use the CPDF back end. Still, it would be good to see the patch. We could conditionally add it for PDFLib users. We could also give some support to CPDF by converting to PNG using imagick/gmagick if installed. Unfortunately, this doesn't really get us where we want to be. The closest I've seen to what we need is prawn-svg, but it's coded in Ruby. Everything I've seen coded in PHP relies on an external app/library. |
@bsweeney I agree with you. But this patch may be a good start (even if it doesn't have all the "hard" code we would need). We could start a separate project like I did with php-font-lib, and get inspiration from fabric.js which includes a pretty good SVG parser + canvas renderer. PDFKit.js has also a part of what we want. |
Hi guys including PhenX, :) There is API in PDFLIB. My work is just implementing this API to DOMPDF. Not too much subliminal. Here it is. I can attach also the other patch to use with CPDF(using imagick). Btw, my editor erases blank spaces after each line so the patch is longer. Excuse me for this. |
@oliverhuynh what's the status of SVG support? I cleaned up the patch file a bit, removing all whitespace changes. It currently looks like this: 20930f43c12f33e143fa306db5570742f715b9c3
sites/all/libraries/dompdf/dompdf_config.inc.php | 6 +-
.../all/libraries/dompdf/include/functions.inc.php | 157 +++++++++++----------
.../libraries/dompdf/include/image_cache.cls.php | 45 +++---
.../dompdf/include/pdflib_adapter.cls.php | 78 ++++++----
4 files changed, 157 insertions(+), 129 deletions(-)
diff --git a/sites/all/libraries/dompdf/dompdf_config.inc.php b/sites/all/libraries/dompdf/dompdf_config.inc.php
index 0c3f8e3..70c0686 100755
--- a/sites/all/libraries/dompdf/dompdf_config.inc.php
+++ b/sites/all/libraries/dompdf/dompdf_config.inc.php
@@ -4,7 +4,7 @@
* @link http://www.dompdf.com/
* @author Benj Carson <benjcarson@digitaljunkies.ca>
* @author Helmut Tischer <htischer@weihenstephan.org>
- * @author Fabien M�nager <fabien.menager@gmail.com>
+ * @author Fabien Ménager <fabien.menager@gmail.com>
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
* @version $Id: dompdf_config.inc.php 468 2012-02-05 10:51:40Z fabien.menager $
*/
@@ -164,7 +164,7 @@ def("DOMPDF_ENABLE_FONTSUBSETTING", false);
* @link http://www.ros.co.nz/pdf
* @link http://www.php.net/image
*/
-def("DOMPDF_PDF_BACKEND", "CPDF");
+def("DOMPDF_PDF_BACKEND", "PDFLib");
/**
* PDFlib license key
@@ -285,7 +285,7 @@ def("DOMPDF_ENABLE_JAVASCRIPT", true);
*
* @var bool
*/
-def("DOMPDF_ENABLE_REMOTE", false);
+def("DOMPDF_ENABLE_REMOTE", TRUE);
/**
* The debug output log
diff --git a/sites/all/libraries/dompdf/include/functions.inc.php b/sites/all/libraries/dompdf/include/functions.inc.php
index 8e0ab02..335409a 100644
--- a/sites/all/libraries/dompdf/include/functions.inc.php
+++ b/sites/all/libraries/dompdf/include/functions.inc.php
@@ -12,6 +12,7 @@ if ( !defined('PHP_VERSION_ID') ) {
$version = explode('.', PHP_VERSION);
define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
}
+define ('IMAGETYPE_SVG', 111);
/**
* Defined a constant if not already defined
@@ -744,36 +745,44 @@ function imagecreatefrombmp($filename) {
/**
* getimagesize doesn't give a good size for 32bit BMP image v5
*
* @param string $filename
* @return array The same format as getimagesize($filename)
*/
function dompdf_getimagesize($filename) {
static $cache = array();
if ( isset($cache[$filename]) ) {
return $cache[$filename];
}
list($width, $height, $type) = getimagesize($filename);
if ( $width == null || $height == null ) {
- $data = file_get_contents($filename, null, null, 0, 26);
-
- if ( substr($data, 0, 2) === "BM" ) {
+ $data = file_get_contents($filename, null, null, 0, 1024);
+ if (strstr($data, 'http://www.w3.org/2000/svg')) {
+ $type = IMAGETYPE_SVG;
+ $width = explode('width="', $data);
+ $width = explode('"', $width[1]);
+ $width = $width[0];
+ $height = explode('height="', $data);
+ $height = explode('"', $height[1]);
+ $height = $height[0];
+ }
+ elseif ( substr($data, 0, 2) === "BM" ) {
$meta = unpack('vtype/Vfilesize/Vreserved/Voffset/Vheadersize/Vwidth/Vheight', $data);
$width = (int)$meta['width'];
$height = (int)$meta['height'];
$type = IMAGETYPE_BMP;
}
}
return $cache[$filename] = array($width, $height, $type);
}
/**
* Converts a CMYK color to RGB
*
* @param float|float[] $c
* @param float $m
* @param float $y
diff --git a/sites/all/libraries/dompdf/include/image_cache.cls.php b/sites/all/libraries/dompdf/include/image_cache.cls.php
index 7d7e560..c8d7c69 100644
--- a/sites/all/libraries/dompdf/include/image_cache.cls.php
+++ b/sites/all/libraries/dompdf/include/image_cache.cls.php
@@ -49,28 +49,28 @@ class Image_Cache {
$message = null;
$remote = ($protocol && $protocol !== "file://") || ($parsed_url['protocol'] != "");
$data_uri = strpos($parsed_url['protocol'], "data:") === 0;
$full_url = null;
$enable_remote = $dompdf->get_option("enable_remote");
try {
// Remote not allowed and is not DataURI
if ( !$enable_remote && $remote && !$data_uri ) {
throw new DOMPDF_Image_Exception("DOMPDF_ENABLE_REMOTE is set to FALSE");
- }
}
+
// Remote allowed or DataURI
else if ( $enable_remote && $remote || $data_uri ) {
// Download remote files to a temporary directory
$full_url = build_url($protocol, $host, $base_path, $url);
// From cache
if ( isset(self::$_cache[$full_url]) ) {
$resolved_url = self::$_cache[$full_url];
}
// From remote
else {
$tmp_dir = $dompdf->get_option("temp_dir");
@@ -105,30 +105,30 @@ class Image_Cache {
}
}
}
// Not remote, local image
else {
$resolved_url = build_url($protocol, $host, $base_path, $url);
}
// Check if the local file is readable
if ( !is_readable($resolved_url) || !filesize($resolved_url) ) {
throw new DOMPDF_Image_Exception("Image not readable or empty");
}
// Check is the file is an image
else {
list($width, $height, $type) = dompdf_getimagesize($resolved_url);
// Known image type
- if ( $width && $height && in_array($type, array(IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_BMP)) ) {
+ if ( $width && $height && in_array($type, array(IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_BMP, IMAGETYPE_SVG)) ) {
//Don't put replacement image into cache - otherwise it will be deleted on cache cleanup.
//Only execute on successful caching of remote image.
if ( $enable_remote && $remote || $data_uri ) {
self::$_cache[$full_url] = $resolved_url;
}
}
// Unknown image type
else {
throw new DOMPDF_Image_Exception("Image type unknown");
@@ -150,31 +150,32 @@ class Image_Cache {
*/
static function clear() {
if ( empty(self::$_cache) || DEBUGKEEPTEMP ) return;
foreach ( self::$_cache as $file ) {
if (DEBUGPNG) print "[clear unlink $file]";
unlink($file);
}
self::$_cache = array();
}
static function detect_type($file) {
list(, , $type) = dompdf_getimagesize($file);
return $type;
}
static function type_to_ext($type) {
$image_types = array(
IMAGETYPE_GIF => "gif",
IMAGETYPE_PNG => "png",
IMAGETYPE_JPEG => "jpeg",
IMAGETYPE_BMP => "bmp",
+ IMAGETYPE_SVG => 'svg',
);
return (isset($image_types[$type]) ? $image_types[$type] : null);
}
static function is_broken($url) {
return $url === self::$broken_image;
}
diff --git a/sites/all/libraries/dompdf/include/pdflib_adapter.cls.php b/sites/all/libraries/dompdf/include/pdflib_adapter.cls.php
index 4bfe191..bf5251e 100644
--- a/sites/all/libraries/dompdf/include/pdflib_adapter.cls.php
+++ b/sites/all/libraries/dompdf/include/pdflib_adapter.cls.php
@@ -774,13 +774,24 @@ class PDFLib_Adapter implements Canvas {
$img_ext = Image_Cache::type_to_ext($img_type);
if ( !isset($this->_imgs[$img_url]) ) {
- $this->_imgs[$img_url] = $this->_pdf->load_image($img_ext, $img_url, "");
+ if ($img_ext == 'svg') {
+ $this->_imgs[$img_url] = $this->_pdf->load_graphics('auto', $img_url, "");
+ }
+ else {
+ $this->_imgs[$img_url] = $this->_pdf->load_image($img_ext, $img_url, "");
+ }
}
$img = $this->_imgs[$img_url];
$y = $this->y($y) - $h;
- $this->_pdf->fit_image($img, $x, $y, 'boxsize={'."$w $h".'} fitmethod=entire');
+ if ($img_ext == 'svg') {
+ $this->_pdf->fit_graphics($img, $x, $y, '');
+ $this->_pdf->close_graphics($img);
+ }
+ else {
+ $this->_pdf->fit_image($img, $x, $y, 'boxsize={'."$w $h".'} fitmethod=entire');
+ }
}
//........................................................................
@@ -788,7 +799,11 @@ class PDFLib_Adapter implements Canvas {
function text($x, $y, $text, $font, $size, $color = array(0,0,0), $word_spacing = 0, $char_spacing = 0, $angle = 0) {
$fh = $this->_load_font($font);
- $this->_pdf->setfont($fh, $size);
+ // TODO: Raise warning
+ if ($fh != 0) {
+ $this->_pdf->setfont($fh, $size);
+ }
+
$this->_set_fill_color($color);
$y = $this->y($y) - Font_Metrics::get_font_height($font, $size);
@@ -875,6 +890,9 @@ class PDFLib_Adapter implements Canvas {
function get_font_height($font, $size) {
$fh = $this->_load_font($font);
+ if (empty($fh)) {
+ return 0;
+ }
$this->_pdf->setfont($fh, $size); I guess:
I would love to have SVG support, so can the first phase possibly with only support for pdflib and if cpdf is ready, we get the support for that too? |
@juriansluiman your patch got corrupted. I am getting this error when trying to apply it with git:
Also the patch by @oliverhuynh doesn't apply anymore. I re-rolled it against the current master branch while removing all unnecessary whitespace changes:
|
When trying to embed a svg I get this error:
|
@luksak can you attach the image here? |
This is the source:
|
The unknown error is because currently DOMPDF doesn't support SVG. You need to apply the patch (if possibile) to get a support only made by PDFLib. Anyway, it would be cool to have it integrated also with ImageMagick/GraphicsMagick to get it just a little automated :) |
I tried applying the patch, just to see if it would work and I'm afraid it still didn't let me put in SVG files. c'est la vie. But I'm going to add a +1 to request support of SVG files, inline or otherwise. |
@DasUmlaut |
@hakimio |
+1 |
+1 for SVG support.. |
+1 |
For your information, I'm currently working on a library to parse and write SVG files onto PDF documents, it will be used by Dompdf. Here is what will be possible : http://pxd.me/dompdf/dompdf_svg.pdf You can follow the project here : https://github.com/PhenX/php-svg-lib |
@Asimov500 for something like that it might help to start a new issue with php-svg-lib. |
My problems with svg are solved, but I thought I should share something with you. If you save out svgs from illustrator for dompdf you will sometimes get a black and white image. This is down to the css properties of the svg being imcompatible with dompdf. So the solution is this When saving svgs |
I'm wondering if there are any plans to support thanks |
It is supported. Maybe, you are encoding it wrong. Did you try to render it in your browser? |
yes browser renders it just fine. here is my image http://138.197.105.143/test |
There is sth wrong with your message formatting. |
Yes sorry about that. I was editing my comment and putting my image in a separate test file. URL is here http://138.197.105.143/test here is my composer
|
I searched FOREVER until I finally came up with my own solution. I thought I should share in case it could be useful to anyone else. If you are using PHP the best solution to inline SVG images (for me) is to output it using BASE64_ENCODE() on the actual svg, then wrapping it in an image using the src as follows:
The only issue you might find is weird dimensions on the actual image. You will have to mess with it on the PDF output to make sure it displays as desired. I needed to remove the width and height of the actual SVG to make sure it was sizing correctly. Again hope this helps! |
@lov2code Thanks that works! But i found svg needs to have defined color trough stroke or fill or it will render transparent. |
This helped me as well. Could not understand why my SVG wasn't showing. Adding a fill fixed it. |
Here you guys have a direct way to do it directly from your controller. In your Controller $path_to_image = "/img/yourimage.svg";
$logo = "data:image/svg+xml;base64,". base64_encode(file_get_contents(public_path($path_to_image)));
$pdf = PDF::loadView('qr', ['data' => $data, 'logo' => $logo]); In your PDF / Blade Template <img src="{{ $logo }}" width="150px" height="100px"> |
This from @Gkiokan helped me. Thanks ) |
What could be wrong with my SVG? it's not displayed
|
@alex-malyita your issue appears to be different from this one. For now you can work around the problem by removing the fill declaration from the SVG element.
Edit: this is likely due to an outdated version of php-svg-lib. Have you tried updating to Dompdf 2.0.2? |
Original author: mar...@mgscreativa.com (June 29, 2011 12:30:48)
What would you like dompdf to do:
Be able, like tcpdf, to render SVG tags into PDF
Do you have an example:
Using TCPDF
PHP Code http://www.tcpdf.org/examples/example_058.phps
PHP Code Execution http://www.tcpdf.org/examples/example_058.pdf
Original issue: http://code.google.com/p/dompdf/issues/detail?id=314
The text was updated successfully, but these errors were encountered: