SVG to PDF in php - php

I have created a PDF using the SVG data.
try{
$mpdf = new mPDF;
$final_html = '<div>';
//$raw_images contains the SVG Path
foreach($raw_images as $rimage){
$final_html .= "<img src='".$rimage."' width='100%'><br/><br/>";
}
$file_name = uniqid($prefix).".pdf";
$final_html .= '</div>';
$mpdf->WriteHTML($final_html);
$mpdf->Output($folder_name.$file_name, 'F');
} catch (ImagickException $ex) {
echo $ex->getMessage();
}
Please check this Codepen For SVG
Now the resulted PDF looks like this View PDF
If you check the SVG data it contains one image with SVG.like <image id="qr_code" xlink:href="data:image/svg+xml.... So SVG data has one svg image in it (means kind of svg inside svg).
SVG image woks fine, means you can view the output (i.e. QR code is visible). But the resulted PDF doesn't display the QR code (might be because QR code image is SVG).
So how to fix this issue, so QR code become visible in the PDF?

See: https://mpdf.github.io/what-else-can-i-do/images.html
Embedded image data can be used either in elements or in CSS
background. gif, png and jpeg are supported.
So, SVG is not supported. Always read the documentation. A tip on the same page:
mPDF partially supports SVG images, including as embedded HTML

Related

CMYK Mode after generating a PDF of images in DOMPDF Laravel

I am working on HTML to PDF generation, i am using laravel with DOMPDF library, I have 3 different images, i have that image through css and html, its working fine, but after when we downloading the file and opening into Photoshop its show RGB mode
I want it to show cmyk Mode.
This is the output file of my pdf
My PDF download code after rendering the html
$customPaper = array(0,0,4500,2950);
$pdf->setOptions(['dpi'=>300]);
$pdf->loadHTML($html)->setPaper($customPaper)->setWarnings(false)->save($filename.'.pdf');
return $pdf->stream();

Trying to embed a base64 JPG in a SVG to post to twitter

In http://cheapbotsdonequick.com/ I'm trying to make a bot that publishes images. It uses SVG and when I use some placeholder service like placecage dot com (that gives me a JPG) the thing runs OK and the tweet goes through with the image. I did a placeholder script and the image appears in CBDQ but the tweet appears with a question mark. The SVG needs the image encoded as base64. I started to make a simpler one, just to convert and use it in the svg tag, but it doesn't work:
svg <svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.1\" width=\"1024\" height=\"512\" style=\"position: relative; background: white;\"><image x=\"0\" y=\"0\" width=\"1024\" height=\"482\" xlink:href=\"/x/myimagescript.php\" id=\"img\" /></svg>
Code:
<?php
// PHP placeholder images
header('Content-type: image/jpeg');
$data = file_get_contents("image_placeholders/44721_tiger_lg.jpg");
$base64 = 'data:image/jpg;base64,' . base64_encode($data);
//$jpegimage = imagecreatefromjpeg($base64);
//imagejpeg($jpegimage);
echo $base64;
?>
So, how can I create a proper image to embed into a SVG that could be posted to twitter?
Thanks a lot for any help.

SVG in mPDF (PHP library )

In php code I use mPDF library for generating PDF.
I am facing a problem with insert svg into PDF
The svg file I catch via POST:
$svg = $_POST['structureSVG'];
$svg_pdf = str_replace('"', '\'', $svg); //change " to '
proof what is inside:
var_dump($svg_pdf);
//shows string which contains:
<svg>...</svg>
here I can be sure, that the SVG was captured correctly. So I put the SVG:
$html = " <div> $svg_pdf </div> ";
$mpdf -> WriteHTML($html);
$mpdf -> Output('pdf/test_svg.pdf', 'I');
but unfortunately the SVG picture is not rendered in PDF
thank You for any help
You need to handle the SVG as regular <img> tag. From my point of view, it'd be easiest to save the file to the temp directory and then let it load with mPDF:
<img src="path/to/the/temp/file.svg">
Unfortunately, you can not use data uri for SVG images (at least in version 5.7.4).

Imagick can't write SVG files

I use Imagick to open an image, resize it, set an image format (if it's needed) and write in another folder the result. It works with ICO and PNG, but can't write SVG files, saying that
ImagickException: Unable to write the file: C:\Sites\devdesktop\test\sites\default\files\tempimages\abstract-spiral-striped-background-100.svg in Imagick->writeimage() (line 44 of C:\Sites\devdesktop\test\sites\all\modules\testmodule\testmodule.module).
It was an error message from Drupal 7.
Here is my code:
$file = file_load($form_state['values']['fid']);
$image = new Imagick(drupal_realpath($file->uri));
$image->setImageFormat($type[$type_num]);
$image->resizeImage($size[$size_num], $size[$size_num], Imagick::FILTER_UNDEFINED, 1);
$destination = 'public://tempimages/'.substr($file->filename, 0, -4).'-'.$size[$size_num].'.'.$type[$type_num];
$image->writeImage(drupal_realpath($destination)); //<---problem
drupal_goto(base_path().'/sites/default/files/tempimages/'.substr($file->filename, 0, -4).'-'.$size[$size_num].'.'.$type[$type_num]);
This code snippet works perfectly with ICO and PNG, but don't write a file in SVG format except a little peace of code
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="200" height="200">
<g style="</svg>
Why so?
SVG isn't an image in the same way that JPG or PNG are.
It's just an xml with plotted points in most cases, and is typically void of dimension and resolution.
The width and height of 200 are just the size in px at which it was saved. So you should never need to 'resize' an svg, as it will scale infinitely.
If you put an SVG in an img tag and define the dimensions, like:
<img src="myawesome.svg" width="500" height="500" />
Even though it's defined in the SVG as having a dimensions of 200, it should scale larger or smaller with no change in the quality of appearance.
Because of this complete difference in structure, it will not follow the same rules in some cases as jpg or png will as they are rendered images.
If this is being done in Drupal; make an exception for SVG that just stores the file without resizing, and add the dimensions where ever it is printed for all images. It won't hurt the rendered images, and it should show the SVG as the same size as them.
TL;DR SVG is an xml file, not really an image, resizing it is pointless since it carries no real resolution or size and can scale infinitely. They only appear to be similar since they are both visual elements. Make an exception for SVG so it just stores the file, and when either the rendered images or the SVG is printed, add the resized dimensions to the img output.

Adjust Image if page break occurs in mpdf library

I am generating pdf from html source using mpdf library in php and everything seems working perfect.
Now I have an issue with the images. Suppose before page end I'm inserting an image but image is big so that it doesn't fit at the bottom of first page and goes to second page. Now I have a long white space at the end of first page because image moved to second page.
Now I want is "if next item to insert in pdf is an image then calculate the remaining size of pdf page if it is less than Image size then adjust the image size so that image can be fit in the pdf page instead of moving to next page" how can i do this here?
Please check the issue image :
If any one has other solution please help me to sort out.
Here's My sample code
include_once 'simple_html_dom.php'; //import html dom and mpdf library
include 'PDFScript/MPDF/mpdf.php';
$mpdf = new mPDF('','','','',15,15,30,15,8,8); //create mpdf object
$html = new simple_html_dom(); //create html dom object
$html = file_get_html("htmlsource.html"); //htmlsource.html is a webpage can contain any html data
$mpdf->WriteHTML($html); //write html source to pdf
$mpdf->Output(); //generate pdf
I got a solution from mpdf forum itself. If Anyone also has the same problem enclose every image in your html inside table as table has a autosize feature in mpdf library.
For more information please check here

Categories