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

Embedded SVG in HTML Capability #320

Open
PhenX opened this issue Dec 6, 2012 · 100 comments
Open

Embedded SVG in HTML Capability #320

PhenX opened this issue Dec 6, 2012 · 100 comments

Comments

@PhenX
Copy link
Member

PhenX commented Dec 6, 2012

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

@PhenX
Copy link
Member Author

PhenX commented Dec 6, 2012

From marclaporte on January 06, 2012 01:09:54
Related link:
https://dev.tiki.org/Online+Publishing+House+-+SVG+Drawings#SVG_to_PDF_conversion

@PhenX
Copy link
Member Author

PhenX commented Dec 6, 2012

From fabien.menager on January 12, 2012 20:13:43
I would also add
http://www.godisaduck.com/svg2pdf_with_fpdf

@PhenX
Copy link
Member Author

PhenX commented Dec 6, 2012

From ger...@bourdin.name on September 28, 2012 10:22:10
why not using imagick to convert svg files in png format, as a quick workaround to this feature ?
I am using dompdf for several years now, i would be glad to contribute.

@PhenX
Copy link
Member Author

PhenX commented Dec 6, 2012

From mar...@mgscreativa.com on September 28, 2012 14:06:53
imagick certainly converts to PNG, but is a very basic convertion and no all hosting services provide it. In my case, my hosting provider refused to install needed addons for imagick to be able to convert SVG to PNG.

@oliverhuynh
Copy link

I have fixed this issue in my local. If there is anyone who needs this patch, please let me know

@bsweeney
Copy link
Member

@oliverhuynh how have you gone about SVG support? We would be interested in knowing the details.

@olivier-weka
Copy link
Contributor

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.

@PhenX
Copy link
Member Author

PhenX commented Mar 21, 2014

@oliverhuynh I totally agree, if you need help pushing to a Git clone, please tell us.

@oliverhuynh
Copy link

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.

@bsweeney
Copy link
Member

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.

@PhenX
Copy link
Member Author

PhenX commented Mar 23, 2014

@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).
@oliverhuynh can you send us the patch please ?

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.
The PDF is basically like a Canvas, we already have a part of the methods required in Cpdf : line, curve, ellipse, text, transformations, etc. The "path" is still missing, but I think it's not really hard to add.
Something harder is integrating gradients, but still not impossible.

@oliverhuynh
Copy link

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.
...
...
...
Oops, the patch is not able to be uploaded.
Try this link pls.http://media.jufist.org/oliver/svginpdflib.patch

@juriansluiman
Copy link

@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:

  1. The config changes could be ignored for review
  2. There is a change in the braces around a try/catch statement in image_cache.cls.php, I have no idea why it's there

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?

@luksak
Copy link

luksak commented Jun 20, 2014

@juriansluiman your patch got corrupted. I am getting this error when trying to apply it with git:

fatal: corrupt patch at line 133

Also the patch by @oliverhuynh doesn't apply anymore. I re-rolled it against the current master branch while removing all unnecessary whitespace changes:

diff --git a/dompdf_config.inc.php b/dompdf_config.inc.php
index 51fbcb5..a195cf9 100644
--- a/dompdf_config.inc.php
+++ b/dompdf_config.inc.php
@@ -160,7 +160,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
@@ -281,7 +281,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/include/functions.inc.php b/include/functions.inc.php
index 8e0ab02..a304235 100644
--- a/include/functions.inc.php
+++ b/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
@@ -758,9 +759,17 @@ function dompdf_getimagesize($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'];
diff --git a/include/image_cache.cls.php b/include/image_cache.cls.php
index 7d7e560..dfb00b9 100644
--- a/include/image_cache.cls.php
+++ b/include/image_cache.cls.php
@@ -121,7 +121,7 @@ class Image_Cache {
         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 ) {
@@ -170,6 +170,7 @@ class Image_Cache {
       IMAGETYPE_PNG  => "png",
       IMAGETYPE_JPEG => "jpeg",
       IMAGETYPE_BMP  => "bmp",
+      IMAGETYPE_SVG => 'svg',
     );

     return (isset($image_types[$type]) ? $image_types[$type] : null);
diff --git a/include/pdflib_adapter.cls.php b/include/pdflib_adapter.cls.php
index 4bfe191..4787332 100644
--- a/include/pdflib_adapter.cls.php
+++ b/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);

@luksak
Copy link

luksak commented Jun 20, 2014

When trying to embed a svg I get this error:

Image type unknown test.svg

@oliverhuynh
Copy link

@luksak can you attach the image here?

@luksak
Copy link

luksak commented Jun 20, 2014

This is the source:

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 17.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     width="44px" height="44px" viewBox="0 0 44 44" enable-background="new 0 0 44 44" xml:space="preserve">
<image display="none" overflow="visible" opacity="0.2" width="44" height="44" xlink:href="cart.png" >
</image>
<rect fill="#66FF66" width="44" height="44"/>
<path fill="#FFFFFF" d="M17.5,30.8c-2.4,0-4.3,1.9-4.3,4.3s1.9,4.3,4.3,4.3s4.3-1.9,4.3-4.3S19.9,30.8,17.5,30.8z M30.6,30.8
    c-2.4,0-4.3,1.9-4.3,4.3s1.9,4.3,4.3,4.3s4.3-1.9,4.3-4.3S33,30.8,30.6,30.8z M37.1,9.1H13.3l-0.6-2.2c-0.4-2-1.7-2.2-2.2-2.2H6.7
    c-1.2,0-2.2,1-2.2,2.2s1,2.2,2.2,2.2h2.2l4.3,15.6c0.4,1.3,1.4,1.7,2.2,1.7h17.4c0.9,0,1.7-0.5,2.2-1.7l3.7-11.3
    c0.3-1.1,0.6-1.6,0.6-2.2C39.2,9.8,38,9.1,37.1,9.1z M31.2,22.1H17l-2.4-8.7h19.5L31.2,22.1z"/>
</svg>
``

@julianxhokaxhiu
Copy link

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 :)

@RobMayer
Copy link

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.

@hakimio
Copy link

hakimio commented Jan 15, 2015

@DasUmlaut
You need to buy PDFLib for this to work...

@RobMayer
Copy link

@hakimio
Welp - I feel dumb now. Thanks for the help.
Still hoping that domPDF will support svg files via CPDF in the future.

@Mte90
Copy link

Mte90 commented Feb 3, 2015

+1

@simplenotezy
Copy link

+1 for SVG support..

@TomasDePomas
Copy link

+1

@PhenX
Copy link
Member Author

PhenX commented May 4, 2015

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
A few fixes need to be done (like text rendering but it's getting good).

You can follow the project here : https://github.com/PhenX/php-svg-lib

@bsweeney
Copy link
Member

@Asimov500 for something like that it might help to start a new issue with php-svg-lib.

@Asimov500
Copy link

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
Click more options and change css properties to "Presentation Attributes". Then it doesn't matter if you svg is transparent or not, or anything. The svg will then be in full colour in dompdf. I hope this helps somebody else. I am using illustrator CC2015 and this method works. The other solution is to use an older version of illustrator.

@bsweeney bsweeney modified the milestone: 0.8.1 May 21, 2017
@N1Prop
Copy link

N1Prop commented May 26, 2017

I'm wondering if there are any plans to support
<img src="data:image/png;base64,..."> (png)
or is it supported already? getting nothing in my PDF

thanks

@hakimio
Copy link

hakimio commented May 26, 2017

It is supported. Maybe, you are encoding it wrong. Did you try to render it in your browser?

@N1Prop
Copy link

N1Prop commented May 28, 2017

yes browser renders it just fine. here is my image http://138.197.105.143/test

@hakimio
Copy link

hakimio commented May 28, 2017

There is sth wrong with your message formatting.

@N1Prop
Copy link

N1Prop commented May 28, 2017

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

"require": {
        "php": ">=5.6.4",
        "laravel/framework": "5.4.*",
        "laravel/tinker": "~1.0",
        "laravelcollective/html": "5.4.*",
        "cartalyst/sentinel": "2.0.*",
        "cviebrock/eloquent-sluggable": " ^4.0.3",
        "cviebrock/eloquent-taggable": "2.1",
        "yajra/laravel-datatables-oracle": "^7.0",
        "intervention/image" :"dev-master",
        "doctrine/dbal": "~2.3",
        "infyomlabs/laravel-generator": "1.0",
        "infyomlabs/core-templates": "5.3.x-dev",
        "infyomlabs/swagger-generator": "dev-master",
        "jlapp/swaggervel": "dev-master",
        "infyomlabs/generator-builder": "dev-master",
        "barryvdh/laravel-dompdf": "^0.8",
        "phenx/php-svg-lib": "0.2.*"
    },

@lov2code
Copy link

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:

$svg = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 36 36" class="circular-chart">
    <path class="circle-bg"
          stroke-width="1"
          fill="none"
          stroke="#ddd"
          d="M18 2.0845
             a 15.9155 15.9155 0 0 1 0 31.831
             a 15.9155 15.9155 0 0 1 0 -31.831"
          />
</svg>';

$html = '<img src="data:image/svg+xml;base64,'.base64_encode($svg).'"  width="100" height="100" />';

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!

@Samuell1
Copy link

@lov2code Thanks that works! But i found svg needs to have defined color trough stroke or fill or it will render transparent.

@Starlette
Copy link

@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.

@bsweeney bsweeney modified the milestones: dompdf-next, 1.0.4, 1.1.0 Jul 9, 2021
@Gkiokan
Copy link

Gkiokan commented Jan 24, 2022

Here you guys have a direct way to do it directly from your controller.
Remember the SVG have to have fill or stroke otherwise you get a transparent SVG.
An Example for Laravel user would be as follow.

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">

@aircokol
Copy link

aircokol commented Nov 13, 2022

Remember the SVG have to have fill or stroke otherwise you get a transparent SVG.

This from @Gkiokan helped me. Thanks )

@alex-malyita
Copy link

alex-malyita commented Feb 4, 2023

What could be wrong with my SVG? it's not displayed

<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <path d="M3.18273 9.00943C2.0794 6.79522 3.19265 4.35157 5.31397 3.07877C6.3353 2.46598 7.65528 2.73192 8.35963 3.6924L9.92229 5.82331C10.5874 6.73027 10.6893 7.93307 10.1864 8.93904L9.77663 9.75849C9.65802 9.99571 9.63927 10.2701 9.7606 10.506C9.98164 10.9356 10.4524 11.6971 11.3592 12.6038C12.266 13.5106 13.0274 13.9814 13.4571 14.2024C13.6929 14.3237 13.9673 14.305 14.2045 14.1864L15.024 13.7766C16.0299 13.2737 17.2327 13.3756 18.1397 14.0407L20.2706 15.6034C21.2311 16.3077 21.497 17.6277 20.8842 18.649C19.6114 20.7704 17.1678 21.8836 14.9536 20.7803C13.0874 19.8504 10.7028 18.3114 8.17717 15.7858C5.65156 13.2602 4.11261 10.8756 3.18273 9.00943Z" fill="#538689"/>
    <path d="M3.18273 9.00943C2.0794 6.79522 3.19265 4.35157 5.31397 3.07877C6.3353 2.46598 7.65528 2.73192 8.35963 3.6924L9.92229 5.82331C10.5874 6.73027 10.6893 7.93307 10.1864 8.93904L9.77663 9.75849C9.65802 9.99571 9.63927 10.2701 9.7606 10.506C9.98164 10.9356 10.4524 11.6971 11.3592 12.6038C12.266 13.5106 13.0274 13.9814 13.4571 14.2024C13.6929 14.3237 13.9673 14.305 14.2045 14.1864L15.024 13.7766C16.0299 13.2737 17.2327 13.3756 18.1397 14.0407L20.2706 15.6034C21.2311 16.3077 21.497 17.6277 20.8842 18.649C19.6114 20.7704 17.1678 21.8836 14.9536 20.7803C13.0874 19.8504 10.7028 18.3114 8.17717 15.7858C5.65156 13.2602 4.11261 10.8756 3.18273 9.00943Z" fill="white" fill-opacity="0.1"/>
</svg>

@bsweeney
Copy link
Member

bsweeney commented Feb 4, 2023

@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.

<svg width="24" height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <path d="M3.18273 9.00943C2.0794 6.79522 3.19265 4.35157 5.31397 3.07877C6.3353 2.46598 7.65528 2.73192 8.35963 3.6924L9.92229 5.82331C10.5874 6.73027 10.6893 7.93307 10.1864 8.93904L9.77663 9.75849C9.65802 9.99571 9.63927 10.2701 9.7606 10.506C9.98164 10.9356 10.4524 11.6971 11.3592 12.6038C12.266 13.5106 13.0274 13.9814 13.4571 14.2024C13.6929 14.3237 13.9673 14.305 14.2045 14.1864L15.024 13.7766C16.0299 13.2737 17.2327 13.3756 18.1397 14.0407L20.2706 15.6034C21.2311 16.3077 21.497 17.6277 20.8842 18.649C19.6114 20.7704 17.1678 21.8836 14.9536 20.7803C13.0874 19.8504 10.7028 18.3114 8.17717 15.7858C5.65156 13.2602 4.11261 10.8756 3.18273 9.00943Z" fill="#538689"/>
    <path d="M3.18273 9.00943C2.0794 6.79522 3.19265 4.35157 5.31397 3.07877C6.3353 2.46598 7.65528 2.73192 8.35963 3.6924L9.92229 5.82331C10.5874 6.73027 10.6893 7.93307 10.1864 8.93904L9.77663 9.75849C9.65802 9.99571 9.63927 10.2701 9.7606 10.506C9.98164 10.9356 10.4524 11.6971 11.3592 12.6038C12.266 13.5106 13.0274 13.9814 13.4571 14.2024C13.6929 14.3237 13.9673 14.305 14.2045 14.1864L15.024 13.7766C16.0299 13.2737 17.2327 13.3756 18.1397 14.0407L20.2706 15.6034C21.2311 16.3077 21.497 17.6277 20.8842 18.649C19.6114 20.7704 17.1678 21.8836 14.9536 20.7803C13.0874 19.8504 10.7028 18.3114 8.17717 15.7858C5.65156 13.2602 4.11261 10.8756 3.18273 9.00943Z" fill="white" fill-opacity="0.1"/>
</svg>

Edit: this is likely due to an outdated version of php-svg-lib. Have you tried updating to Dompdf 2.0.2?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests