SVG in mPDF (PHP library ) - php

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

Related

SVG to PDF in 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

could not load image in html2pdf in codeigniter

I am trying to create a pdf file from html in my codeigniter project. The html file contains an image but it will not load in the pdf. I am using html2pdf library and here is my code.
example.php :
<img src="<?php echo base_url()?>assets/images/logo.jpg" alt="BulkSMS">
I am pasting only the <img> tag here, because it is a large file.
and here is my controller function
public function htmlToPdf($data) {
$this->html2pdf->folder(PURCHASE_INVOICE_PATH);
$fileName = $data['invoiceNumber'].'.pdf';
$this->html2pdf->filename($fileName);
$this->html2pdf->paper('a4', 'portrait');
$content = $this->load->view('templates/example', '', true);
//Load html view
$this->html2pdf->html($content);
$this->html2pdf->create('save');
return $fileName;
}
I tried to change path of image to full path and type of image from png to jpg. But still it not showing
This issue is happening becuase of CORS. Uncomment The following line from the dompdf_config.inc.php file located in application/libraries/dompdf folder.
define("DOMPDF_ENABLE_REMOTE", true);

CSS and Images not displaying while converting HTML to PDF in CodeIgniter

I am new to CodeIgniter, trying to convert HTML to PDF. Text is almost same in both html_page_output and pdf_output but CSS and images are not working in PDF.
My controller code is like this:
$html = $this->load->view('searching/pdfShow', $data,true);
$this->load->library('dompdf_gen');
$this->dompdf->load_html($html);
$this->dompdf->render();
$this->dompdf->stream("welcome.pdf");
Check the URLs you have in src attribute. It most likely should be absolute, not like "/images/foo/bar.png" but "http://yourdomain.com/images/foo/bar.png"

mpdf Skips CSS when i render the view into HTML?

i am using codeigniter i am working on invoice and i want to print a PDF file of my invoice page here is my controller.
$pdfFilePath ='invoice.pdf';
$data=$this->singleinvoice($invoiceId);
ini_set('memory_limit','32M');
$html = $this->load->view('invoice/invoicetopdf', $data, true);
$this->load->library('pdf');
$pdf = $this->pdf->load();
$pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822));
$pdf->WriteHTML($html);
$pdf->Output($pdfFilePath, 'D');
redirect("invoice");
when i echo $html veriable is show fine but when it output the pdf .it has no css styling i mean it convert the HTML without any of my css Style.
i also tried the following but no luck.
$style=file_get_contents(base_url().'_assets/css/bootstrap.css');
$pdf->WriteHTML($style,1);
$pdf->Output($pdfFilePath, 'D');
Now i want to convert my HTML page to PDF as it is shown to the public with complete styling and markups.
does it possible any suggestion or solution ?
you have to convert your Page Structure from DIV to Traditional Table structure.
arrange your elements contents etc.into table or tables.
then try some basic CSS.
because most of CSS are not supported in mpdf or any other pdf converter.
Click here
to know what(CSS) is supported and what is not in mpdf.

how to include graphs into html2pdf?

I am using maxChart.class.php to create Graphs and its working properly.
I am also using html2pdf.class.php to create Online pdf report. I tried to include chart in pdf file but its not working.
My code is
$rowval=$sqcat1->qcategory;
$data3["$rowval"]=$per;
$content = "
<page>
$content.=$mc3 = new maxChart($data3);
$mc3->displayChart('',1,450,400,true);
"
</page>";
require_once("/pdf1/html2pdf.class.php");
$html2pdf = new HTML2PDF('P','A4','en');
$html2pdf->WriteHTML($content);
$html2pdf->Output('demo.pdf');
How can i add graphs in file ?
Anybody please help me.
I used MPDF and SVGGraphs to do this
Code to add graphs value in php variable
$html.='<br>'.$graph->Fetch("CylinderGraph",true).'<br>';
Code for MPDF
include("/mpdf.php");
$mpdf=new mPDF('c');
$mpdf->SetHTMLHeader($header);
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
You can do the following:
make the chart an image and embed that image in the document, using <img src=...
display the chart image using binary data <img src="data:image/png;base64,binarycontentherebase64encoded...">

Categories