I am trying to convert a SVG file to PDF file using TCPDF library in PHP. I have created a SVG file and use PHP to replace text and plan to render the resultant SVG file to PDF file.
Any idea, if TCPDF library supports SVG to PDF conversion. Any pointers in this direction would really help me.
You don't really need to replace text or render, Once you have created your svg file. All you just need to include tcpdf in your script and create its object like e.g.
require_once(DOCUMENT_ROOT . '/library/Lib/tcpdf/mypdf.php');
$this->pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$this->pdf->AddPage(); // Add page to pdf before addding content
//There are several other property need to be set on basis of your need
$this->pdf->ImageSVG('file/mySVGFile.svg', 15, 20, '', '', '',
'', '', 1, false); // 15,20 are co-ordinate to position graph in pdf
Once you added your content to your pdf, Last step comes is to download the pdf using .
$this->pdf->Output('my.pdf', 'FD');
Feel free to ask for anything you have any query in this code.
Try
$pdf->ImageSVG(VIEW_JS."graph_as_svg/".php", '', '', '210', '100', '', '', '', 0, false);
Related
I am trying to use a custom font in my tcpdf file.
$pdf->addTTFfont('../../../fonts/RedHatDisplay-Regular.ttf', '', '', 32);
addTTFfont does:
TCPDF_FONTS::addTTFfont($fontfile, $fonttype, $enc, $flags, $outpath, $platid, $encid, $addcbbox);
This is my Code.
It just gives me weird chars when creating the pdf. The thing is, if I use the RedHatDisplay-Italic.ttf file, it just works perfect. Do you have any ideas what I can do?
So I had the same issue with the Quicksand font. The issue was the font itself (or the resource where I got it from).
I tried multiple resources, because in the past I downloaded Google Fonts (like Lato) and they worked.
So for Quicksand the downloaded font from
https://google-webfonts-helper.herokuapp.com/fonts failed
https://www.fontmirror.com/quicksand worked
CAUTION It looks like if the font already exists, it won't be overwritten by TCPDF_FONTS::addTTFfont so you need to manually get rid of an existing font first (probably from previous adding in vendor/tecnickcom/tcpdf/fonts) .
Other than that, the following code just works fine:
Only call this code once to add the font
<?php
$fontnames = [];
$fontnames[] = TCPDF_FONTS::addTTFfont('fonts/Quicksand Bold.ttf');
$fontnames[] = TCPDF_FONTS::addTTFfont('fonts/Quicksand Regular.ttf');
// Make sure you use the real font name. Different file names may (or may not) produce different font names.
die(print_r($fontnames, 1));
This code then for generating the pdf
<?php
$pdf = new TCPDF('P', 'mm', 'A4');
$pdf->SetFont('quicksand'); // This is the fontname from above
$pdf->AddPage();
$txt = '0123456789 ABCDEFGHIJKLMNOPQRSTUVQXYZ';
// print a block of text using Write()
$pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0);
$pdf->Output('test.pdf', 'I');
exit(1);
I would like to add a custom font to a pdf I'm generating using TCPDF. I might be missing something but the docs seem to be out dated. They are referencing the addTTFfont() function but I think it's been deprecated and no longer exists in the latest version of TCPDF.
I read that I need to convert the ttf file and put it in the fonts folder so I ran:
php/tcpdf/tools/tcpdf_addfont.php -i php/font/rumpelstiltskin-webfont.ttf
and it generated these files which are now in the fonts folder:
rumpelstiltskinwebfont.ctg.z
rumpelstiltskinwebfont.z
rumpelstiltskinwebfont.php
Then I tried to add the font:
$pdf->addFont('rumpelstiltskin');
$pdf->SetFont('rumpelstiltskin', '', 14, '', false);
but I'm getting an error:
TCPDF ERROR: Could not include font definition file: rumpelstiltskin
I figured out my issue, I was almost there.
Here is a step by step:
First convert your font using the tcpdf_addfont.php tool font in the TCPDF tools folder:
php/tcpdf/tools/tcpdf_addfont.php -i php/font/rumpelstiltskin-webfont.ttf
This will generate the required files and put them in the TCPDF fonts folder. Check the fonts folder and copy the name of the font, in my case it was rumpelstiltskinwebfont.
In your code set the font using the font file's name and write a line of text:
$pdf->SetFont('rumpelstiltskinwebfont', '', 14, '', false);
$pdf->Write(0, 'Fill text', '', 0, '', true, 0, false, false, 0);
That's it. Hope this helps someone. :)
Got this answer in another question and resolved for me. You just need to use the first parameter, the path of the font file. Worked with TTF and OTF fonts.
It generates a name string to use with $pdf->SetFont($fontname, '', $font_size);
Hope it helps.
The latest TCPDF version automatically convert fonts into TCPDF format using the addTTFfont() method. For example:
// convert TTF font to TCPDF format and store it on the fonts folder
$fontname = TCPDF_FONTS::addTTFfont('/path-to-font/FreeSerifItalic.ttf', 'TrueTypeUnicode', '', 96);
// use the font
$pdf->SetFont($fontname, '', 14, '', false);
For further information and examples, please check the TCPDF Fonts documentation page.
NOTE: Once the font has been converted, TCPDF no longer requires the TTF file or the above call to addTTFfont()!
You can convert ttf fonts with http://fonts.palettize.me and then put uncompressed result in fonts folder into tcpdf class. Then you can add it with $pdf->SetFont('rumpelstiltskinwebfont', '', 14); using the name of the file
Since TCPDF (version 6.2.6) you can use TCPDF_FONTS::addTTFfont directly instead of AddFont.
TCPDF makes three files out of the ttf and puts them into the directory "font" of TCPDF. Once these have been created, they can be used for all future PDFs.
Example:
First (for simplicity) I created a folder in the font folder of TCPDF with the name of the new font, in my example "arialuni" and copied my ttf of this font into it.
If I want to create my font files in a single separate file that does not use Composer autoload, I have to include TCPDF once and also specify the correct path to my ttf.
require_once('vendor/tecnickcom/tcpdf/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$fontname = TCPDF_FONTS::addTTFfont('vendor/tecnickcom/tcpdf/fonts/arialuni/arialuni.ttf', 'TrueTypeUnicode', '', 32);
I'm trying to add images on top of an existing PDF.
The pdf contains a blank grid, I am writing a script that will insert images on top of the PDF and output a new modified PDF file.
I am using FPDI and TCPDF libraries to load and edit the PDF file.
Any image or text i try to add using Write(); and Image(); functions does not appear anywhere in the Output file.
<?php
// defining encoding and linking libraries
header('Content-Type: text/html; charset=utf-8');
require_once('tcpdf/tcpdf.php');
require_once('fpdi/fpdi.php');
// Creating new page with PDF as a background
$pdf = new FPDI();
$pdf->setSourceFile("resources/worksheet_template.pdf");
$tplIdx = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($tplIdx, 0, 0, 0, 0, true);
// $pdf->MultiCell(0, 5,'$pdf', 0, 'J', 0, 2, '', '', true, 0, false);
$pdf->Image('resources/lantern.png', 50, 50, 100, '', '', 'http://www.tcpdf.org', '', false, 300);
ob_clean();
$pdf->Output('WorksheetTest.pdf', 'I');
?>
This script works without problems! Expecting that all resources are accessible. The MultiCell call looks something special but as you'd used single quotes at least the string $pdf will appear in the resulting document. Furthermore the header() is obsolete too. Make sure that your PHP file is saved without a BOM to get rid of the ob_clean() call, too.
Generally it should be a good practice to define a font and size before writing any text but it seems that TCPDF handles this internally by a standard font for you.
Also make sure that you are using the latest version of both FPDI and TCPDF.
I'm using this extension in Yii:
http://www.yiiframework.com/extension/tcpdf/
I would like to write to a pdf file with an external font, for example this:
http://www.1001fonts.com/open-sans-font.html
Here is my code:
$pdf = Yii::createComponent('application.extensions.tcpdf.ETcPdf', 'P', 'cm', 'A4', true, 'UTF-8');
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("Some Body");
$pdf->SetTitle("TCPDF Example 002");
$pdf->SetSubject("TCPDF Tutorial");
$pdf->SetKeywords("TCPDF, PDF, example, test, guide");
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->AddPage();
$fontRegular = $pdf->addTTFfont(Yii::getPathOfAlias('webroot') . '/OpenSans-Regular.ttf');
$pdf->SetFont($fontRegular, "", 30);
$pdf->Cell(0,10,"Example",1,1,'C');
$pdf->Output("example_002.pdf", "I");
It works, I can read the pdf many programs, except Corel Draw 6X. When I'm opening the pdf it starts with a message of "missing fonts" and I can't replace them any other kind of fonts. Can anyone tell me how should I add the ttf font, or embed it into the pdf?
Thanks in advance!
So the problem is for Corel Draw . You have to Install the font.
1)Download your ttf font from here
2)Extract it and copy the font to C:\Windows\Fonts
3) Restart Corel Draw and make sure the new font is available by creating a text field and selecting the font.
Reference
I am using tcpdf to produce PDF's from HTML. Everything is working fine and when I view the PDF on my computer I can see it just fine, but for some reason when I look at the PDF on my iPhone it shows up blank. All I can see are the borders I created that contain values in them but there are no values showing.
Here is my code
require_once('/tcpdf/tcpdf/config/lang/eng.php');
require_once('/tcpdf/tcpdf/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setLanguageArray($l);
$pdf->SetFont('dejavusans', '', 10);
$pdf->AddPage();
$html = file_get_contents('http://www.website.com/invoice.php?invoice_id='.$invoice_id);
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();
$pdf->Output('/html/admin/emailattachements/invoice.pdf', 'F');
In that last line. I copy the PDF to that directory when I grab it later with an email script and sends it off to a customer.
Edit: SOLVED
I discovered it was the font I was using to produce the PDF. iPhone's can't read dejavusans :) I changed it to 'times' and it works fine
Edit: Update
Since this article I have had to create many more PDF's with tcpdf and while I can't really explain why some fonts were not working while others where I recently applied some of the suggestions over at http://www.tcpdf.org/fonts.php and applied
$fontname = $pdf->addTTFfont('/path-to-font/DejaVuSans.ttf', 'TrueTypeUnicode', '', 32);
By adding a font manually and setting the font file path and uploading the file manually I was able to get existing font's that did not work and actually get them to work.
Change the below lines:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetFont('dejavusans', '', 10);
To:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);
$pdf->SetFont('helvetica', '', 10, '', true);
And then check.
Not sure how its working for you without the AddFont method, but I had the same issue and adding the fourth param $subset = false fixes it.
$subset (mixed) if true embedd only a subset of the font (stores only
the information related to the used characters); if false embedd full
font; if 'default' uses the default value set using
setFontSubsetting(). This option is valid only for TrueTypeUnicode
fonts. If you want to enable users to change the document, set this
parameter to false. If you subset the font, the person who receives
your PDF would need to have your same font in order to make changes to
your PDF. The file size of the PDF would also be smaller because you
are embedding only part of a font.
$pdf->AddFont('dejavusans', '', 'dejavusans', false);
$pdf->AddFont('dejavusans', 'B', 'dejavusans', false);
$pdf->SetFont('dejavusans', '', 10);
dejavusans fixes a number of issues with multibyte characters so not possible to simple change to helvetica. By making this change we found the PDF to more than double in size.
This is kinda the same idea as #user3548394 but only have to make this change once.
Check the font type you are using, is like html, but it won't fallback.
Create pdf with static text and check.