Reputation: 1294
so I was tasked with switching our code from using Imagick to tcpdf for dynamically converting svgs to pdfs.
I've ended up with following code:
<?php
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/../../../../../app/Mage.php';
require_once MAGENTO . '/../../../../../lib/tcpdf/tcpdf.php';
//require_once MAGENTO . '/../../../../../lib/tcpdf/config/lang/eng.php';
$app = Mage::app('default');
Mage::getSingleton("core/session", array("name" => "frontend"));
$session = Mage::getSingleton("customer/session");
$session_id = $session->getEncryptedSessionId();
$base = str_replace("/", "\/", $_POST['base']);
$b_svg = $_POST['b_svg'];
$t_svg = $_POST['t_svg'];
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('von Eicken');
$pdf->SetTitle('');
$pdf->SetSubject('');
$pdf->SetKeywords('');
$pdf->SetCellPadding(0);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
if (!empty($b_svg))
{
$b_svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'.$_POST['b_svg'];
$b_svg = preg_replace('#'.$base.'media#', '../../../../../media', $b_svg);
$b_svg = urldecode($b_svg);
$pdf->ImageSVG($file=@$b_svg, $x=0, $y=0, $w='', $h='', $link='', $align='', $palign='', $border=0, $fitonpage=false);
}
if (!empty($t_svg))
{
if (!empty($b_svg)){$y=50;}
$t_svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'.$_POST['t_svg'];
$t_svg = preg_replace('#'.$base.'media#', '../../../../../media', $t_svg);
$t_svg = urldecode($t_svg);
$pdf->ImageSVG($file=@$t_svg, $x=0, $y, $w='', $h='', $link='', $align='', $palign='', $border=0, $fitonpage=false);
}
$pdf->output("./pdfs/tmp_".$session_id.".pdf", F);
echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN)."frontend/von_eicken/default/php/pdfs/temp_".$session_id.".pdf";
?>
I know it's a bit messy, it's in the process of conversion.
I was pleased that tcpdf seems to work really simple, however, the created pdfs don't show anything, just a single vertical line at the very top of a blank page. No error message either. I wonder if I am missing something in my code or whether my svgs need to have some certain configuration.
I'd like to try to avoid having to plug the svgs appart to do the conversion manually, so can someone explain to me, or point me to a good tutorial, how to use the ImageSVG functionality of tcpdf properly?
Thanks a lot.
p.s. I do appreciate hints to clean up the code, too
p.p.s. Oh, btw, the third require is commented out because the file wasn't in the tcpdf package I downloaded and I figured the examples might refer to an older version. But if I really need that file, where do I get it?
Upvotes: 1
Views: 996
Reputation: 1294
Ok, my mistake was obviously that I assumed that a new PDF object would start out with a blank page.
But it does not start out with any page at all, $pdf->addPage()
is needed to create a page in the first place where my SVGs can be drawn on.
Frustrating little neglectances. Sorry to have bothered you all.
Upvotes: 1