0
votes

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.

2
$stream = "http://localhost/pdf/getPDF.php?fid=2"; (??) - Roy Bogado
Consider not to store binary data into the database. Just store the filepath into the database and put the file on disk. - Philipp Palmtag

2 Answers

0
votes

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

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!