I have been trying to create a PDF containing a SVG image with the TCPDF Library. I wanted to have the SVG centered on a page with some text above and below it.
I have managed to get this working to a point however part of the SVG image is being cut off, it seems to have something to do with the X/Y position but I am not sure how to correct this problem.
What is causing the TCPDF Library error that cuts off my SVG image?
Code:
<?php
require_once('../../TCPDF/tcpdf.php');
// Remove the default header and footer
class PDF extends TCPDF {
public function Header() {}
public function Footer() {}
}
$pdf = new PDF();
$pdf->AddPage();
$pdf->SetFont('helvetica', 'B', 160);
$pdf->SetTextColor(0,0,0);
$pdf->Cell(0, 0, 'Higher', 1, 1, 'C', false, '', 1);
$pdf->Ln(4);
$pdf->ImageSVG($file='Image.svg', $x=0, $y=80, $w=100, $h=100, $link='', $align='N', $palign='C', $border=1, $fitonpage=false);
$pdf->Ln(4);
$pdf->Cell(0, 0, 'Lower', 1, 1, 'C', 0, '', 1);
$file = 'Result' . '.pdf'; // Set the name of the PDF file
$pdf->Output($file, 'F'); // Save PDF to file
$pdf->Output($file, 'I'); // Display the PDF
?>
This seems to a bug with the current version of the TCPDF Library that I was using.
The problem is when you use the align features:
etc, and if anyone else has a similar problem and needs a workaround for it I fixed it by manually centering the SVG using the x/y positions.
Another problem to note is when I tried using some SVG images that were in large size I noticed it would create visual glitches so the SVG handling definitely seems to have a few bug. For that problem the workaround I found was to lower the size of the SVG and then scale them using the width/height values instead which seemed to correct the problem.