Adding images over PDF using FPDI and TCPDF - php

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.

Related

tcpdf custom font weird characters

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

Creating a cell shadow effect in fpdf

I want to emulate generating a "sticky note" in PDFs. My current system is a wordpress website with a custom plugin written in PHP that uses fpdf for PDF generation. I can generate a yellow rectangle with borders and text easily:
$pdf = new fpdf();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->SetFillColor(255, 246, 108);
$pdf->SetXY(0, 40);
$pdf->ShadowCell($pdf->GetPageWidth(),50,'Hello World!', 1, 0, 'C', true);
$pdf->Output();
Now the issue is generating a shadow around the box. I've gone through the fpdf API and examples, but didn't see anything relevant that I could use. There is a text-shadow example, but that doesn't work in my case.

TCPDF font not embadding, showing "..." on adobe reader

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('');

SVG to PDF conversion using TCPDF in PHP

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

Can TCPDF / FPDI accept PDF as string?

Is it possible to feed TCPDF or FPDI PDFs as a string? I have an incoming array of PDFs as strings, and I can't write to disk. I wasn't able to find anything in the documentation about this.
If not, is there an efficient way to store/read these PDFs from memory or as objects? as to feed them to FPDI?
If you look at the setSourceFile method documentation, you will see that you can also pass a resource or a StreamReader. What is very interesting with the StreamReader is that it also shares a createByString method. So you can use it like this:
use setasign\Fpdi\PdfParser\StreamReader;
//...
$myData = ... ;
$stream = StreamReader::createByString($myData);
$pdf->setSourceFile($stream);
//...
This will avoid any code duplication... hope this helps someone in the future...
FPDI does not accept strings, but TCPDI, which I just released, has a setSourceData() method in addition to FDPI's setSourceFile(), as I happened to have the exact same requirement. TCPDI has its own parser (tcpdi_parser, based on TCPDF's parser) which supports PDFs above 1.4 without requiring the commercial addon for FPDI - which may also be of benefit when working with existing PDFs.
you can used stream wraper ,..
you can write wraper.php from class link above
header('Content-Type: text/html; charset=utf-8');
require_once('tcpdf/tcpdf.php');
require_once('fpdi/fpdi.php');
require_once('wraper.php');
// Creating new page with PDF as a background
$pdf = new FPDI();
$varInAnyScope = file_get_contents('proposal0.pdf');
$pdf->setSourceFile(VarStream::createReference($varInAnyScope));
$tplIdx = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($tplIdx, 0, 0, 0, 0, true);
$pdf->Write(0, "Stack overflow");
ob_clean();
$pdf->Output('WorksheetTest.pdf', 'I');

Categories