everyone I am working with fpdf an fpdi, is my first work and I have a problem. I can not edit a pdf. This is the problem. thanks
Class setasign\Fpdi\FpdfTpl not found in C:\wamp\www\Ale\fpdi\src\Fpdi.php
use setasign\Fpdi\Fpdi;
require_once('fpdf/fpdf.php');
require_once('fpdi/src/Fpdi.php');
require_once('fpdi/src/autoload.php');
// initiate FPDI
$pdf = new Fpdi();
// add a page
$pdf->AddPage();
// set the source file
$pdf->setSourceFile('documento.pdf');
// import page 1
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at position 10,10 with a width of 100 mm
$pdf->useTemplate($tplIdx, 10, 10, 100);
// now write some text above the imported page
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(30, 30);
$pdf->Write(0, 'This is just a simple text');
$pdf->Output('newDoc.pdf','F');
You cannot edit a PDF with FPDI!
Remove the line:
require_once('fpdi/src/Fpdi.php');
...to give the later required autoload function a chance.
Maybe not related to this exact question but I had a similar situation in my Lumen project...
I resolved it by reintsalling vendor files. I hope if someone is facing similar situation, I hope this helps.
Related
I know this question has been asked many times but I read all related answer and my problem is not solved yet. I can add text and image on a new blank pdf file by code below.
<?Php
require('fpdf.php');
$pdf = new FPDF('P','mm','A4');
$pdf->AddPage();
/* $pdf->setSourceFile("test1.pdf"); */
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(0, 5);
$pdf->Write(0, 'This is just a simple text');
$pdf->Image('sample.png',100,0);
$pdf->Output('file.pdf','F');
?>
But when I uncomment the line /* $pdf->setSourceFile("sourse.pdf"); */ to add text and image on existing pdf, I get Uncaught Error: Call to undefined method FPDF::setSourceFile() .
Also when add
require('fpdi.php');
$pdf = new FPDI('P','mm','A4');
I get Uncaught Error: Class "setasign\Fpdi\FpdfTpl" not found .
How can I solve this?
You have to set source file and after that import page from source file to your new created file by this way you can edit it. Please find more details from https://www.webniraj.com/2016/09/12/creating-editing-a-pdf-using-php/
// Create new Landscape PDF
$pdf = new FPDI('l');
// Reference the PDF you want to use (use relative path)
$pagecount = $pdf->setSourceFile( 'certificate.pdf' );
// Import the first page from the PDF and add to dynamic PDF
$tpl = $pdf->importPage(1);
$pdf->AddPage();
// Use the imported page as the template
$pdf->useTemplate($tpl);
// render PDF to browser
$pdf->Output();
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 am creating a pdf document using tcpdf which is going well. The issue I am having is that I want to include an external pdf in the middle of the document and then continue to add my own pages afterwards.
I have read that FPDI is the best way to achieve this but I am stuck with trying to implement a solution. All of the examples I have found seem to revolve around using an external pdf as a background or template to the entire document, not just as an insert into a document.
Any help would be gratefully received.
AddPage() Method generates a blank page. Each call generates only 1 page. You need to call AddPage() before useTemplate(); After that you can still add new context.
$pdf = new FPDI();
$pdf->AddPage();
$pdf->AddFont('courier');
$pdf->Write(10, 'page 1 created by TCPDF');
$pages = $pdf->setSourceFile('middle.pdf');
for($i=0; $i<$pages; $i++)
{
$pdf->AddPage();
$tplIdx = $pdf->importPage($i+1);
$pdf->useTemplate($tplIdx, 10, 10, 200);
}
$pdf->AddPage();
$pdf->Write(10, 'page 2 created by TCPDF');
I'm using FPDF. The class is implemented and working in its most basic form. I created a business card (3.5" x 2") and placed it on an 8.5" x 11" page. The card is replicated 10 times on a single page.
Here is what I'm trying to do:
Replicate a variable 10 times and define a custom offset for each (string prints at the bottom of each card)
Change the page size from $unit='mm', $size='A4' to $unit='inch', $size='letter'. Doing so gives me an error FPDF error: Incorrect unit: inch
The code I'm working with is below:
switch($_POST['picker']){
case 'option1':
// initiate FPDI
$pdf = new FPDI();
// add a page
$pdf->AddPage();
// set the sourcefile
$pdf->setSourceFile('10-up.pdf');
// import page 1
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at point 10,10 with a width of 100 mm
$pdf->useTemplate($tplIdx, 0, 0, 216, 279);
// now write some text above the imported page
$pdf->SetFont('Arial');
$pdf->SetTextColor(0,0,0);
$pdf->SetXY(12, 12);
//This is the variable I want to repeat 10 times and define custom offsets for each
$pdf->Write(0, $user);
$pdf->Output('final.pdf', 'D');
break;
The documentation I've read seems really limited. If you know of any good documentation, please let me know. Thanks.
In case anyone else is having the same problem, here is the answer for the second part of my question:
You have to set the page size here...
$pdf->AddPage('P', 'Letter');
This defines the page as "Portrait" , "Letter"
I'm still looking for an answer on part one. If anyone can help, I'd appreciate it.
There is this undocumented function in the class FPDF also called FPDF.
function FPDF($orientation='P', $unit='mm', $size='A4') { ... }
I used it this way (– please note that the $pdf object is a child class of FPDF class. This could, for instance, be an FPDI object; When it is defined as an FPDI object, it is defined in the same way as in your original question, where $pdf is an FPDI object):
$pdf = new FPDI();
$pdf->FPDF('L' /* =$orientation */, 'pt' /* =$unit */, 'A4' /* =$size */);
So, in this code example an object of a class that is derived from the FPDF class is calling the FPDF function of the FPDF class to set the orientation of the PDF document and sets the unit to 'pt' (= points) and the size to DIN A4, which is the standard letter format in Germany.
You do it where you initiate the constructor, where you currently have $pdf = new FPDI()
$pdf = new FPDF('P', 'in', 'Letter');
P - sets the document up as portrait
in - sets the unit up as inches
Letter - sets the document size as US Letter
http://www.fpdf.org/en/doc/fpdf.htm
Background info:
I got a project to produce a customised PDF on the fly from a given PDF file using PHP. All I need it to do is to replace strings, e.g. search in "template.pdf" for "{Address}", replace with "Street Name".
I've seen links to fpdf/pdfi/dompdf etc., but can't find any useful example code that I could use :s.
Any help / pointers would be greatly appreciated.
fpdf is fantastic, you need to use somthing else to import an exisitng PDF though, See below.
http://www.setasign.de/products/pdf-php-solutions/fpdi/
require_once('fpdf.php');
require_once('fpdi.php');
$pdf =& new FPDI();
$pagecount = $pdf->setSourceFile('TestDoc.pdf');
$tplidx = $pdf->importPage(1, '/MediaBox');
$pdf->addPage();
$pdf->useTemplate($tplidx, 10, 10, 90);
$pdf->Output('newpdf.pdf', 'D');
decided to generate html web page (PHP) then use wkhtmltopdf
(http://code.google.com/p/wkhtmltopdf)
to produce the pdf bit of a work around but less hastle
PDFlib (with the additional PDI) from pdflib.com should be able to do this for you. Admittedly it is pretty pricey, so there may be other options, too :)