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();
Related
I am doing a basic system that the staff uploads a pdf file with some description and this data stored in database MySQL.
The admin will view this pdf and click on approval if everything is ok.
An image will be inserted in pdf file with approve logo.
I use fpdf and fpdi class to do this, I manage to do this if PDF file stored on the actual path as shown in the code below.
<?php
use setasign\Fpdi\Fpdi;
require_once('fpdf/fpdf.php');
require_once('fpdi2/src/autoload.php');
// initiate FPDI
$pdf = new Fpdi();
// add a page
$pdf->AddPage();
// set the source file
$pdf->setSourceFile('PdfDocument.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();
BUT when I try to use $pdf->setSourceFile($string) or other that actual file for example (PDF $content from string (database) or URL) I cannot manage to do that.
// set the source file
//$pageCount = $pdf->setSourceFile("http://localhost/pdf/getpicture.php?fid=2");
$stream = fopen('data:text/plain,' . urlencode("http://localhost/pdf/getPDF.php?fid=2"), 'rb');
//$reader = new SetaPDF_Core_Reader_Stream($stream);
$pageCount = $pdf->setSourceFile($stream);
My question is how can I import PDF from MySQL string to be edited by fpdf and fpdi or any other free PDF classes.
Note: I try to use stream_wrapper_register with no luck so far. as in this link
https://www.setasign.com/support/faq/miscellaneous/using-a-pdf-from-a-php-variable-instead-of-a-file/
Please help me with a simple example as I am not really familiar with PDF classes.
Thank you.
I think the source of your trouble is in the line:
$stream = fopen('data:text/plain,' . urlencode("http://localhost/pdf/getPDF.php?fid=2"), 'rb');
PHP's fopen function returns a file pointer, and is not giving you the name of the PDF file you want.
So later when you call
$pageCount = $pdf->setSourceFile($stream);
$stream is not a string with a PDF filename.
If your http://localhost/pdf/getPDF.php?fid=2 URL is returning the filename of the PDF, try getting that value with file_get_contents like so:
$pdf_file = file_get_contents('http://localhost/pdf/getPDF.php?fid=2');
and then call
$pdf->setSourceFile($pdf_file);
You should not use an additional HTTP request to access a file from a database!
FPDI 2 allows you to read from any source through a StreamReader class:
// use a resource
$fh = fopen('a/path/to/a.pdf', 'rb');
$pdf->setSourceFile(new StreamReader($fh));
// same as
$pdf->setSourceFile($fh);
// don't forget to call fclose($fh);
// use a path
$path = 'a/path/to/a.pdf';
$pdf->setSourceFile(StreamReader::createByFile($path));
// same as
$pdf->setSourceFile($path);
// use a string
$pdfString = '%%PDF-1.4...';
$pdf->setSourceFile(StreamReader::createByString($pdfString));
So do not call an external script but query your database for the PDF and pass it e.g. as a string.
PS: You cannot edit a PDF with FPDI!
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.
I'm able to generate a PDF with PHP using FPDF, but I'm trying to create an on-screen preview of the generated PDF rather than jumping straight to the online PDF viewer.
<?php
use setasign\Fpdi\Fpdi;
use setasign\Fpdi\PdfReader;
require_once('fpdf/fpdf.php');
require_once('fpdi2/src/autoload.php');
$pdf = new Fpdi();
$pageCount = $pdf->setSourceFile('pdf1.pdf');
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++)
{
$tplIdx = $pdf->importPage($pageNo);
// add a page
$pdf->AddPage();
$pdf->useTemplate($tplIdx, 0, 30);
$src = $target_file;
$pdf->Image($src,170,0, 30, 30);
}
$pdf->Output();
?>
The below code is what I've got at the moment, but i'm stuck on trying to generate it on the same page.
$pdf->Output();
The above segment seems to generate the PDF automatically onto a new page (opens online PDF viewer). I've searched through documentation and forums but can't seem to find anything.
Any help would be greatly appreciated. Thanks!
FPDF creates a PDF file. A PDF file needs a viewer or reader application, which renders the content of the PDF document. You cannot just output it e.g. between some HTML fragments on a website.
So your question is not related to FPDF or FPDI at all but you're asking for a PDF viewer/render which you can use on your website.
The most popular solution for such task would be pdf.js.
For other possible canditates you may try "html5 pdf viewer" on google.
I am trying to create direct pdf of webpage. so i am using fpdf for that.
I have kept fpdf files on fpdf folder and created a separate create-pdf.php file with following code :
<?php
require('fpdf.php');
$url = $_GET[$url1];
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,$url);
$pdf->Output();
?>
And used following php code to get current url of page of which pdf is to be created.
<?php
$url1 = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
?>
And Created a Link to create pdf as follows :
Create PDF
Now pdf is getting created in new tab...but It is Empty / blank...No text is there on created pdf...
I am trying to import a pdf document with FPDI and add some text to it but when I try to open the new pdf get the error message "format error: not a PDF or corrupted"!
Here is the code:
require_once ('fpdf.php');
require_once ('fpdi.php');
// initiate FPDI
$pdf = new FPDI();
// add a page kl
$pdf->AddPage();
// set the sourcefile
$pdf->setSourceFile('doc1.pdf');
// import page 1
$tplIdx = $pdf->importPage(1);
// use the imported page
$pdf->useTemplate($tplIdx);
// now write some text above the imported page
$pdf->SetFont('Arial');
$pdf->SetTextColor(255,0,0);
$pdf->SetXY(25, 25);
$pdf->Write(0, "This is just a simple text");
$pdf->Output('newpdf.pdf', 'D');
I downloaded this code from the FPDI website and customized this.
The PDF version of my file is 1.3 . (The documentation says FPDI can work with PDFs up to version 1.5)
Tried older versions of FPDI and fpdf_tpl.php but still get this error.
I couldn't find the answer neither with GOOGLE nor in this forum!
Any help appreciated!
UPDATE
In the meantime I found the problem!
All the files were up to date except FPDF! I used an old version of FPDF. Now I use 1.7 instead of 1.5.
from the website, FPDF has to be version 1.6 or below to work with FPDI