2

I have a PDF of a blank certificate, I want to fill in two lines when the user completes a course of study, and display the PDF so they can print or download it.

I am using TCPDF to write the two lines on an existing PDF.

Per suggestions in a previous post ( using PHP to fill in a PDF ), I am using TCPDF_IMPORT to bring an existing PDF into the object, and writing on top of it.

However, the original PDF does NOT show on the screen, I am left with a blank document, with only the lines of text I created.

Below is what I have to this point - it yields ONLY the line "This is my test text."

<?php
// Include the main TCPDF library (search for installation path).
require_once( '../tcpdf_import.php' );

// create new PDF document
$pdf = new TCPDF_IMPORT( '1_cert.pdf' );

// set document information
$pdf->SetCreator( 'aaa.com' );
$pdf->SetAuthor( 'aaa.com' );
$pdf->SetTitle( 'Certificate Test' );
$pdf->SetSubject( 'In completion of x-module' );
$pdf->SetKeywords( '' );

// set default monospaced font
$pdf->SetDefaultMonospacedFont( PDF_FONT_MONOSPACED );

// set font
$pdf->SetFont( 'times', 'B', 30 );

// display
$pdf->SetDisplayMode( 'fullpage', 'SinglePage', 'UseNone' );

// set margins
$pdf->SetMargins( 10, PDF_MARGIN_TOP, 10 );

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


// -------------------------------------------------------------
// stuff i believe should write test over an existing PDF 
// -------------------------------------------------------------

$pdf->StartPage( 'L', '', false );
$pdf->SetY( 50 );
$pdf->Cell( 0, 0, 'test text', 0, 1, 'C' );
$pdf->EndPage( false );

// -------------------------------------------------------------
// end of stuff i believe should write test over an existing PDF 
// -------------------------------------------------------------

//Close and output PDF document
$pdf->Output( 'aTest.pdf', 'I' );
?>
Community
  • 1
  • 1
j-p
  • 3,698
  • 9
  • 50
  • 93
  • Why don't you save the PDF as a high-quality image and use that as a background? That way you don't have to futz with the import bologna. – Jared Farrish Sep 25 '14 at 01:39
  • I've thought of that jared - but it's a "band-aid" or a "work around" for something I should be able to do. I've found a solution - I'll post it. – j-p Sep 25 '14 at 02:20

3 Answers3

5

Well, not as eloquent as I wanted, but I found something that works....

<?php
require_once "tcpdf/tcpdf.php";
require_once "FPDI/fpdi.php";
$pdf = new FPDI( 'L', 'mm', 'LETTER' ); //FPDI extends TCPDF
$pdf->AddPage();
$pages = $pdf->setSourceFile( 'test.pdf' );
$page = $pdf->ImportPage( 1 );
$pdf->useTemplate( $page, 0, 0 );
$pdf->Output( 'newTest.pdf', 'F' );
?>

Thanks to Simon who posted in http://sourceforge.net/p/tcpdf/discussion/435311/thread/66272894/

I was able to modify this - it entails running two libraries - but it works.

j-p
  • 3,698
  • 9
  • 50
  • 93
  • FPDI natively supports only pdf to version 1.4. If your pdf is above 1.4 you have to purchase a parser licence – Ophiuchus Nov 09 '17 at 14:28
3

Create a file and call it pdfConcat.php and paste:

<?php
require_once("tcpdf/tcpdf.php");
require_once("fpdi/fpdi.php");

class concat_pdf extends FPDI {
     var $files = array();
     function setFiles($files) {
          $this->files = $files;
     }
     function concat() {
          foreach($this->files AS $file) {
               $pagecount = $this->setSourceFile($file);
               for ($i = 1; $i <= $pagecount; $i++) {
                    $tplidx = $this->ImportPage($i);
                    $s = $this->getTemplatesize($tplidx);
                    $this->AddPage('P', array($s['w'], $s['h']));
                    $this->useTemplate($tplidx);
               }
          }
     }
}
?>

Usage:

include_once("pdfConcat.php");
$pdf =& new concat_pdf();

$pdf->setFiles(array("doc.pdf","pauta.pdf", "4bp.pdf", "5bp.pdf"));
$pdf->concat();
$pdf->Output("newpdf.pdf", "I");

http://garridodiaz.com/concatenate-pdf-in-php/

Olè!!!

Oscar Zarrus
  • 790
  • 1
  • 9
  • 17
0

As stated in TCPDF_IMPORT documentation page at this time (2020-04-16)

TCPDF_IMPORT !!! THIS CLASS IS UNDER DEVELOPMENT !!!

Futhermore the free version of FPDI supports PDF up to version 1.4

If someone else is looking for something that works easily with TCPDF, I used TCPDI from https://github.com/pauln/tcpdi. You can find some fork ready for composer too.

The usage is quite simple and similar to FPDI. Here a snipped from my code. I have a privacy policy (a static PDF file) and want to save a copy with user name and agreement date in the footer of each page.

// Create new PDF document
$pdf = new TCPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
...
// Add the pages from the source file.
$pagecount = $pdf->setSourceFile($localPrivacy);
for ($i = 1; $i <= $pagecount; $i++) {
    $tplidx = $pdf->importPage($i);
    $pdf->AddPage();
    $pdf->useTemplate($tplidx);
    // Add agreement text in document footer
    $pdf->SetXY(15,282);
    $pdf->Cell(180, 5, "Documento approvato da {$fullName} il {$date}", 0, 0, 'C');
}
// Send PDF on output
$pdf->Output(FOLDER_PATH . DIRECTORY_SEPARATOR . "{$userId}.pdf", 'F');