I'm using TCPDF to create a PDF file. However, some of the SVG files that I have and would like to use in this PDF turns into grey/ black in the output. If I open those SVG files in the browser or Adobe Illustrator it looks correct.
Code that I have used to create the PDF:
require_once('tcpdf_include.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set some language-dependent strings (optional)
if (#file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->SetFont('helvetica', '', 10);
$pdf->AddPage();
$pdf->ImageSVG($file='images/star.svg', $x=5, $y=5, $w='200', $h=200, $link='', $align='', $palign='', $border=0, $fitonpage=false);
$pdf->Output('example_012.pdf', 'I');
However, another SVG works absolutely fine. I have attached both the SVG file and PDF attachment.
SVG that don't work in PDF: https://www.dropbox.com/s/2kbj2200cw4hhrm/star.svg?dl=0
Output PDF:
https://www.dropbox.com/s/nn8pfpdmzx66a50/mypdf-star.pdf?dl=0
SVG that works correctly in PDF
https://www.dropbox.com/s/fsr6f763n39wflt/tux.svg?dl=0
Output PDF:
https://www.dropbox.com/s/5hs4jo3dz3knyod/mypdf-tux.pdf?dl=0
One of several SVG features that TCPDF doesn't support is setting style characteristics via class names. For example, your star SVG's fill colors won't be used because they are set with a class.
For this particular case, export from Illustrator with inline styles or presentation attributes and it should work.
[Edit: As an addendum, there are numerous ways for an SVG to break in TCPDF. In general, simpler is better. TCPDF's SVG parser is reasonably capable, but far from complete, so some breaking is expected and this is why the option to rasterize with ImageMagick is avaiable.]
Related
I´m trying to generate a PDF file that contains Urdu text. I have tried several fonts like: Jameel Noori Nastaleeq, Alvi Nastaleeq, Arial Unicode, etc. But some characters do not show properly. When I copy the text from the PDF and paste it to another program like Notepad the text is showed correctly.
I´m using this code to convert the .ttf file to TCPDF format.
require_once('TCPDF/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$fontname = TCPDF_FONTS::addTTFfont('includes/Fonts/JameelNooriNastaleeqRegular.ttf', 'TrueTypeUnicode', '', 32);""
That code created three files: jameelnoorinastaleeq.php, jameelnoorinastaleeq.z, jameelnoorinastaleeq.ctg.z.
Then in the PHP page this is my code:
require('TCPDF/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set some language dependent data:
$lg = Array();
$lg['a_meta_charset'] = 'UTF-8';
$lg['a_meta_dir'] = 'rtl';
$lg['a_meta_language'] = 'fa';
$lg['w_page'] = 'page';
// set some language-dependent strings (optional)
$pdf->setLanguageArray($lg);
// ---------------------------------------------------------
// set font
$pdf->SetFont('jameelnoorinastaleeq','',12);
// add a page
$pdf->AddPage();
// Urdu content
$htmlurdu = '<span color="#660000">Urdu example:</span><br />احساسات: اکساہٹ(جوش۔تحریک)؛اعصابی؛
<br />احساسات: اُوپر تک کھدائی کا؛"سوراخ کرنا(نقب لگانا)بل کھودنےیا بنانے کا۔؛جڑ سے اکھاڑنے۔کھود کر نکالنےکا""احساس؛"<br />احساسات: اکڑن(سختی)؛اعضاء(بازو اور ٹانگوں)کے جوڑوں کا؛(بے ڈھنگے پن(اناڑی پن)کا؛)
<br />احساسات: اینٹھن(مروڑ۔کھچاؤ)؛بیرونی(خارجی)طورپر؛';
$pdf->WriteHTML($htmlurdu, true, 0, true, 0);
//Close and output PDF document
$pdf->Output('testUrdu.pdf', 'I');
The Urdu Text is showed like this:
???????: ??????(?????????)????????
I have tried using an external website to convert the JameelNooriNastaleeq.ttf file to TCPDF format. I used this site http://fonts.palettize.me/ then copied the generated files to TCPDF/fonts directory but it didn´t work. Urdu text still not showing properly.
I tried the solution posted here: Urdu supported PDF library compatible with php. But there are some Urdu characters that readability is poor with font ARIAL UNICODE MS. I tried to do the same with font Jameel Noori Nastaleeq, but didn´t work. The PDF shows many spaces and very few characters, when I copy the text to another editor it is showed properly.
I have spent several hours trying to fix this problem. When showing English characters there isn´t any problem, but Urdu is not working. Sometimes it displayed "?" other times showed spaces.
Please help me!
I have a script taken from TCPDF examples I am trying to out put it to another folder but I am unable to. I have tried searching stackoverflow and have used the examples here but it doesnt seem to work for me. I have also tried it on my server and still the same error. I have tried placing the folder in the TCPDF folder also to see if it was path issue.
below is a the sample from Their website
<?php
require_once('tcpdf_include.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 001');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (#file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// ---------------------------------------------------------
// set default font subsetting mode
$pdf->setFontSubsetting(true);
// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 14, '', true);
// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage();
// set text shadow effect
$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));
// Set some content to print
$pdf->Output('TestFolder/example_001.pdf', 'F');
?>
TCPDF ERROR: Unable to create output file:
Sounds like a simple rights issue to me.
Does the output folder exist?
Does your web server run as a different user to the folder where the site is hosted?
To provide a real solution, you need to provide more information about your web-server setup. Which web server do you use? Can you log in via a terminal?
I had rights issues too, after I installed TCPDF via composer. It still causes me issues every time I set set up new development environments.
TCPDF tries to write to its installation folder during execution. If that folder is not writable by your webserver, TCPDF will die.
Try:
su
chmod -R g+w vendor/tcpdf
chown -R owner:apache vendor/tcpdf
Substituting, of course, the folder, owner and apache values with the real values from your server.
make sure that your file('TestFolder/example_001.pdf') is not open anywhere when you run script. if it's open somewhere else then TCPDF can't open it.
I added many fonts in TCPDF using this line of code
TCPDF_FONTS::addTTFfont('fonts/ArchitectsDaughter.ttf', 'TrueTypeUnicode', '', 96);
$pdf->AddFont("ArchitectsDaughter");
Many other font is working but, this one is not working.
When i opening this pdf into reader, it shows error like this
cannot extract the embedded font 'ArchitectsDaughter'. some character
may not display or print correctly.
I am importing svg file in pdf.
Here is the SVG file which i inserting in pdf, and you can get PDF from here and here is the font file.
Here is full code how pdf will generates.
$fileName='export';
$uploadPath = Config::get('constants.paths.uploads.images.base').'/'.$fileName.'.svg';
$pdf = new TCPDF();
TCPDF_FONTS::addTTFfont(dirname(dirname(dirname(dirname(__FILE__)))).'/vendor/font-awesome/fonts/ArchitectsDaughter.ttf', 'TrueTypeUnicode', '', 96);
TCPDF_FONTS::addTTFfont(dirname(dirname(dirname(dirname(__FILE__)))).'/vendor/font-awesome/fonts/Archivor.ttf', 'TrueTypeUnicode', '', 96);
$pdf->AddFont("Archivor");
$pdf->AddFont("ArchitectsDaughter");
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage();
$pdf->ImageSVG($uploadPath, $x='', $y='', $w='', $h='', $link='', $align='', $palign='', $border=0, $fitonpage=true);
$filename = 'export.pdf';
$pdf->output($filename, 'D');
exit;
Other fonts working ok for me. Don't know what happening with some fonts.
What is the solution for this?
According to documentation TCPDF_FONTS::addTTFfont() adds the provided font permanently to the fonts folder and returns its name. So there is no reason to add it every time, but it is necessary to use added font with correct name.
// ...
$pdf = new TCPDF();
$fontArchitectsDaughter = TCPDF_FONTS::addTTFfont(realpath(__DIR__ . '/../../../vendor/font-awesome/fonts/ArchitectsDaughter.ttf'), 'TrueTypeUnicode', '', 96);
$fontArchivor = TCPDF_FONTS::addTTFfont(realpath(__DIR__ . '/../../../vendor/font-awesome/fonts/Archivor.ttf'), 'TrueTypeUnicode', '', 96);
$pdf->AddFont($fontArchivor);
$pdf->AddFont($fontArchitectsDaughter);
// ...
First set up the font via TCPDF_FONTS::addTTFfont() or by adding the necessary files in the fonts dir (convert the TTF file via a TCPDF font converter like http://fonts.snm-portal.com)
After that, activate the font:
$pdf->SetFont('FontAwesome','');
Then, write a unicode character with the writeHTML function, starting with &#x and ending with ;
e.g.: for the f0c9 (bars) icon (http://fontawesome.io/icon/bars/):
$pdf->writeHTML('');
I have been using TCPDF for sometime. It's simple to use, outputs low size PDF and is under active development.
Following is the code for a page which should only have Hello World and a footer showing page number. However I get an additional Horizontal Line at the top of the page.It's bugging me. How do I get rid of it?
<?php
require_once('config/lang/eng.php');
require_once('tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
// set default header data
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
//$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
//$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);//if i comment this out the lower line disappears
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
//$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$pdf->setLanguageArray($l);
// ---------------------------------------------------------
// set font
$pdf->SetFont('helvetica', '', 10);
// add a page
$pdf->AddPage();
// define some HTML content with style
$html = <<<EOF
Hello World
EOF;
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
// reset pointer to the last page
$pdf->lastPage();
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('example_061.pdf', 'I');
?>
Solution:
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
Changing or eliminating Header & Footer in TCPDF
just do this $pdf->setPrintHeader(false); and the line at the top will go away
The horizontal line is defined on the default Header().
You can either override the Header() method as on example n. 3 or disable the header as on the example n. 2.
Check the TCPDF website at http://www.tcpdf.org and consult the official forum for further information.
In case this is not solved for anyone else here and they are using FPDI to import a template, try looking at the calculated page height from FPDI and the resultant page height from TCPDF. For me they did not match, and to get rid of the black line I had to add 8 to the page height, and subtract 7 from the y-ordinate value in the useTemplate function in addition to setPrintHeader(false), like this:
$tplidx = $pdf->ImportPage($i);
$s = $pdf->getTemplateSize($tplidx);
// TCPDF STUFF, AddPage(), etc.
$pdf->useTemplate($tplidx,0,-7,$s['w'],$s['h']+8);
I need to code a PHP script that would let me generate a pdf file which displays a member ID card (something like a credit card used to identify oneself) at a certain resolution.
Let me explain:
I do have the basic blueprint of the card in png file format. The script needs to drop in a member's name and birthday along with a serial. So far, no problem - there are plenty of good working PHP libraries out there.
My problem is to ensure that the resulting pdf (the generated image of the card, to be precise) meets a certain resolution (preferably 300dpi), so that printing it would look right.
Any ideas?
EDIT
I solved it using the TCPDF library which lets you scale images at a certain resolution.
Get it here: http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf
EDIT #Don
require_once(dirname(__FILE__).'/tcpdf/config/lang/eng.php');
require_once(dirname(__FILE__).'/tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT,
true, 'UTF-8', false);
// set document information
$pdf->SetCreator('SO-youth');
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set font
$pdf->SetFont('courier', '', 10);
// add a page
$pdf->AddPage();
// set JPEG quality
$pdf->setJPEGQuality(100);
$cred = <<<EOT
<p>
<b>{$userdata->first_name} {$userdata->last_name}</b><br/>
<span style="font-size: 80%;">{$userdata->geburtstag}</span>
</p>
EOT;
$id = <<<EOT
<span style="font-size: 60%;">{$userdata->club_id}</span>
EOT;
$pdf->Image(dirname(__FILE__).'/img/clubcard.jpg',
10, 10, 85.6, 53.98, null, null, null, false, 300);
$pdf->writeHTMLCell(60, 15, 50.5, 20.5, $cred);
$pdf->writeHTMLCell(50, 20, 77, 50.5, $id);
//Close and output PDF document
$pdf->Output($userdata->filename, 'F');
I would use Imagemagick for this purpose, along with Imagick to be able to access it directly from php.
You will then be able to take the original image, add some text to it, and output (or store) as pdf, with something like this:
$image = new Imagick($filename);
$draw = new ImagickDraw();
$draw->setFont($font);
$draw->setFontSize($fontSize);
$image->annotateImage($draw, $xpos, $ypos, $rotation, $text);
// Changes the dpi
$image->setImageResolution(200,200);
I can't find the correct code for outputting/storing it as pdf quickly, but that should be documented somewhere on the Imagemagick site.