Rectangle 27 0

Insert images files in existing PDF file using PHP?


for($count=0;$count<$doc_list_length;$count++){

    //create FDF file
    $id = rand(11111,99999);
    $filename = $populated_pdf_path . $casenumber.$doc_list[$count].".fdf";    
    $file = fopen($filename,"w");
    $filecontents = "%FDF-1.2". PHP_EOL;
    $filecontents .= "%" . PHP_EOL;
    $filecontents .= "1 0 obj" . PHP_EOL;
    $filecontents .= "<<" .PHP_EOL;
    $filecontents .= "/FDF << /Fields [ ";
    $filecontents .= "<</T(name)/V($name)>>";
    $filecontents .= "] " . PHP_EOL;
    $filecontents .= "/F (empty_pdf/$doc_list[$count].pdf) ";
    $filecontents .= "/ID [ <$id>" . PHP_EOL;
    $filecontents .= "] >> " . PHP_EOL;
    $filecontents .= ">> ". PHP_EOL;
    $filecontents .= "endobj" . PHP_EOL;
    $filecontents .= "trailer" . PHP_EOL;
    $filecontents .= "<<" . PHP_EOL;
    $filecontents .= "/Root 1 0 R" . PHP_EOL . PHP_EOL;
    $filecontents .= ">>" . PHP_EOL;
    $filecontents .= "%%EOF";
    fwrite($file, $filecontents);
    fclose($file);

    //insert image on this document only
    //generate qrcode
    if($doc_list[$count] == "checklist"){

        $qrCodeFileName = $cemetery."qrcode.png";
        if(!file_exists($populated_pdf_path.$qrCodeFileName)){
            include('include/phpqrcode/qrlib.php');  
            $codeContents = "http://www.asuperduperwebaddress.com"; 
            QRcode::png($codeContents, $populated_pdf_path.$qrCodeFileName);
        }

        if(!file_exists($populated_pdf_path.$cemetery."qrcode.pdf")){
            //make pdf with image
            require_once('include/fpdf.php');
            $image = $populated_pdf_path.$qrCodeFileName;
            $pdf = new FPDF();
            $pdf->AddPage();
            $pdf->Image($image, 183, 250, 25, 25, 'PNG' );
            $pdf->Output( $populated_pdf_path.$cemetery."qrcode.pdf");
        }

        //setup file paths for pdf output
        $tempFile = $populated_pdf_path . $casenumber.$doc_list[$count]."temp.pdf";
        $pdfExampleFile = $populated_pdf_path . $casenumber.$doc_list[$count].".pdf"; 
        $pdfFile = $empty_pdf_path . $doc_list[$count] . ".pdf";
        if($doc_list[$count] == "checklist")
        $fdfFile = $filename; 
        $fdfTemplateFile = $filename;

        //fill pdf
        $command = "pdftk $pdfFile fill_form $fdfTemplateFile output $tempFile 2> fill_form.log";
        passthru($command);

        //stamp pdf with qrcode
        $command = "pdftk " . $tempFile . " stamp " . $populated_pdf_path.$cemetery . "qrcode.pdf output " . $pdfExampleFile; 
        passthru($command);
    }
    else{
        //setup file paths for pdf output
        $pdfExampleFile = $populated_pdf_path . $casenumber.$doc_list[$count].".pdf"; 
        $pdfFile = $empty_pdf_path . $doc_list[$count] . ".pdf";
        if($doc_list[$count] == "checklist")
        $fdfFile = $filename; 
        $fdfTemplateFile = $filename;

        //fill pdf
        $command = "pdftk $pdfFile fill_form $fdfTemplateFile output $pdfExampleFile 2> fill_form.log";
        passthru($command);
    }

}

I am using PHP, FPDF to generate a pdf from an FDF file and then insert a qrcode graphic into the pdf as well. I kept running into trouble because my empty pdf has form fields built in and inserting a graphic with other programs erased my form fields. I finally figured it out. Hope this helps someone else who is struggling with the same.

I work for funeral home if any of the var names throw you off and this code isn't polished its been cut and pasted from tons of different sources ;)

Thanks. One change I had to make, a dest parameter is needed for the $pdf->Output call. I used: $pdf->Output("F", $filename);

Note
Rectangle 27 0

Insert images files in existing PDF file using PHP?


for($count=0;$count<$doc_list_length;$count++){

    //create FDF file
    $id = rand(11111,99999);
    $filename = $populated_pdf_path . $casenumber.$doc_list[$count].".fdf";    
    $file = fopen($filename,"w");
    $filecontents = "%FDF-1.2". PHP_EOL;
    $filecontents .= "%" . PHP_EOL;
    $filecontents .= "1 0 obj" . PHP_EOL;
    $filecontents .= "<<" .PHP_EOL;
    $filecontents .= "/FDF << /Fields [ ";
    $filecontents .= "<</T(name)/V($name)>>";
    $filecontents .= "] " . PHP_EOL;
    $filecontents .= "/F (empty_pdf/$doc_list[$count].pdf) ";
    $filecontents .= "/ID [ <$id>" . PHP_EOL;
    $filecontents .= "] >> " . PHP_EOL;
    $filecontents .= ">> ". PHP_EOL;
    $filecontents .= "endobj" . PHP_EOL;
    $filecontents .= "trailer" . PHP_EOL;
    $filecontents .= "<<" . PHP_EOL;
    $filecontents .= "/Root 1 0 R" . PHP_EOL . PHP_EOL;
    $filecontents .= ">>" . PHP_EOL;
    $filecontents .= "%%EOF";
    fwrite($file, $filecontents);
    fclose($file);

    //insert image on this document only
    //generate qrcode
    if($doc_list[$count] == "checklist"){

        $qrCodeFileName = $cemetery."qrcode.png";
        if(!file_exists($populated_pdf_path.$qrCodeFileName)){
            include('include/phpqrcode/qrlib.php');  
            $codeContents = "http://www.asuperduperwebaddress.com"; 
            QRcode::png($codeContents, $populated_pdf_path.$qrCodeFileName);
        }

        if(!file_exists($populated_pdf_path.$cemetery."qrcode.pdf")){
            //make pdf with image
            require_once('include/fpdf.php');
            $image = $populated_pdf_path.$qrCodeFileName;
            $pdf = new FPDF();
            $pdf->AddPage();
            $pdf->Image($image, 183, 250, 25, 25, 'PNG' );
            $pdf->Output( $populated_pdf_path.$cemetery."qrcode.pdf");
        }

        //setup file paths for pdf output
        $tempFile = $populated_pdf_path . $casenumber.$doc_list[$count]."temp.pdf";
        $pdfExampleFile = $populated_pdf_path . $casenumber.$doc_list[$count].".pdf"; 
        $pdfFile = $empty_pdf_path . $doc_list[$count] . ".pdf";
        if($doc_list[$count] == "checklist")
        $fdfFile = $filename; 
        $fdfTemplateFile = $filename;

        //fill pdf
        $command = "pdftk $pdfFile fill_form $fdfTemplateFile output $tempFile 2> fill_form.log";
        passthru($command);

        //stamp pdf with qrcode
        $command = "pdftk " . $tempFile . " stamp " . $populated_pdf_path.$cemetery . "qrcode.pdf output " . $pdfExampleFile; 
        passthru($command);
    }
    else{
        //setup file paths for pdf output
        $pdfExampleFile = $populated_pdf_path . $casenumber.$doc_list[$count].".pdf"; 
        $pdfFile = $empty_pdf_path . $doc_list[$count] . ".pdf";
        if($doc_list[$count] == "checklist")
        $fdfFile = $filename; 
        $fdfTemplateFile = $filename;

        //fill pdf
        $command = "pdftk $pdfFile fill_form $fdfTemplateFile output $pdfExampleFile 2> fill_form.log";
        passthru($command);
    }

}

I am using PHP, FPDF to generate a pdf from an FDF file and then insert a qrcode graphic into the pdf as well. I kept running into trouble because my empty pdf has form fields built in and inserting a graphic with other programs erased my form fields. I finally figured it out. Hope this helps someone else who is struggling with the same.

I work for funeral home if any of the var names throw you off and this code isn't polished its been cut and pasted from tons of different sources ;)

Thanks. One change I had to make, a dest parameter is needed for the $pdf->Output call. I used: $pdf->Output("F", $filename);

Note
Rectangle 27 0

Insert images files in existing PDF file using PHP?


$pdf = new FPDI();
$pdf->AddPage();
$pdf->setSourceFile("MySource");
$template = $pdf->importPage(1);
$pdf->useTemplate($template);
$pdf->Image('MyImage.jpg', $x, $y, $width, $height);
$pdf->Output($outputPath, "F");
$pdf->AddPage();

Hi Tony, I followed the example which you have mentioned and I can create a pdf with images, but even though my original pdf consists of only one page, the created pdf has a first page which is BLANK and in the second page I get the contents which I wanted. Any idea how to eliminate this and get the required changes in the first page. I would also accept your answer if you could sort this out. Thanks

You can place an image using FPDI like so:

thanks tony for pointing it out, answer accepted :) just one more basic question, is it possible to increase/decrease the image size or the image sizes are standard.

Note
Rectangle 27 0

Insert images files in existing PDF file using PHP?


$pdf = new FPDI();
$pdf->AddPage();
$pdf->setSourceFile("MySource");
$template = $pdf->importPage(1);
$pdf->useTemplate($template);
$pdf->Image('MyImage.jpg', $x, $y, $width, $height);
$pdf->Output($outputPath, "F");
$pdf->AddPage();

Hi Tony, I followed the example which you have mentioned and I can create a pdf with images, but even though my original pdf consists of only one page, the created pdf has a first page which is BLANK and in the second page I get the contents which I wanted. Any idea how to eliminate this and get the required changes in the first page. I would also accept your answer if you could sort this out. Thanks

You can place an image using FPDI like so:

thanks tony for pointing it out, answer accepted :) just one more basic question, is it possible to increase/decrease the image size or the image sizes are standard.

Note
Rectangle 27 0

Insert images files in existing PDF file using PHP?


for($count=0;$count<$doc_list_length;$count++){

    //create FDF file
    $id = rand(11111,99999);
    $filename = $populated_pdf_path . $casenumber.$doc_list[$count].".fdf";    
    $file = fopen($filename,"w");
    $filecontents = "%FDF-1.2". PHP_EOL;
    $filecontents .= "%" . PHP_EOL;
    $filecontents .= "1 0 obj" . PHP_EOL;
    $filecontents .= "<<" .PHP_EOL;
    $filecontents .= "/FDF << /Fields [ ";
    $filecontents .= "<</T(name)/V($name)>>";
    $filecontents .= "] " . PHP_EOL;
    $filecontents .= "/F (empty_pdf/$doc_list[$count].pdf) ";
    $filecontents .= "/ID [ <$id>" . PHP_EOL;
    $filecontents .= "] >> " . PHP_EOL;
    $filecontents .= ">> ". PHP_EOL;
    $filecontents .= "endobj" . PHP_EOL;
    $filecontents .= "trailer" . PHP_EOL;
    $filecontents .= "<<" . PHP_EOL;
    $filecontents .= "/Root 1 0 R" . PHP_EOL . PHP_EOL;
    $filecontents .= ">>" . PHP_EOL;
    $filecontents .= "%%EOF";
    fwrite($file, $filecontents);
    fclose($file);

    //insert image on this document only
    //generate qrcode
    if($doc_list[$count] == "checklist"){

        $qrCodeFileName = $cemetery."qrcode.png";
        if(!file_exists($populated_pdf_path.$qrCodeFileName)){
            include('include/phpqrcode/qrlib.php');  
            $codeContents = "http://www.asuperduperwebaddress.com"; 
            QRcode::png($codeContents, $populated_pdf_path.$qrCodeFileName);
        }

        if(!file_exists($populated_pdf_path.$cemetery."qrcode.pdf")){
            //make pdf with image
            require_once('include/fpdf.php');
            $image = $populated_pdf_path.$qrCodeFileName;
            $pdf = new FPDF();
            $pdf->AddPage();
            $pdf->Image($image, 183, 250, 25, 25, 'PNG' );
            $pdf->Output( $populated_pdf_path.$cemetery."qrcode.pdf");
        }

        //setup file paths for pdf output
        $tempFile = $populated_pdf_path . $casenumber.$doc_list[$count]."temp.pdf";
        $pdfExampleFile = $populated_pdf_path . $casenumber.$doc_list[$count].".pdf"; 
        $pdfFile = $empty_pdf_path . $doc_list[$count] . ".pdf";
        if($doc_list[$count] == "checklist")
        $fdfFile = $filename; 
        $fdfTemplateFile = $filename;

        //fill pdf
        $command = "pdftk $pdfFile fill_form $fdfTemplateFile output $tempFile 2> fill_form.log";
        passthru($command);

        //stamp pdf with qrcode
        $command = "pdftk " . $tempFile . " stamp " . $populated_pdf_path.$cemetery . "qrcode.pdf output " . $pdfExampleFile; 
        passthru($command);
    }
    else{
        //setup file paths for pdf output
        $pdfExampleFile = $populated_pdf_path . $casenumber.$doc_list[$count].".pdf"; 
        $pdfFile = $empty_pdf_path . $doc_list[$count] . ".pdf";
        if($doc_list[$count] == "checklist")
        $fdfFile = $filename; 
        $fdfTemplateFile = $filename;

        //fill pdf
        $command = "pdftk $pdfFile fill_form $fdfTemplateFile output $pdfExampleFile 2> fill_form.log";
        passthru($command);
    }

}

I am using PHP, FPDF to generate a pdf from an FDF file and then insert a qrcode graphic into the pdf as well. I kept running into trouble because my empty pdf has form fields built in and inserting a graphic with other programs erased my form fields. I finally figured it out. Hope this helps someone else who is struggling with the same.

I work for funeral home if any of the var names throw you off and this code isn't polished its been cut and pasted from tons of different sources ;)

Note
Rectangle 27 0

Insert images files in existing PDF file using PHP?


$pdf->AddPage();

Hi Tony, I followed the example which you have mentioned and I can create a pdf with images, but even though my original pdf consists of only one page, the created pdf has a first page which is BLANK and in the second page I get the contents which I wanted. Any idea how to eliminate this and get the required changes in the first page. I would also accept your answer if you could sort this out. Thanks

thanks tony for pointing it out, answer accepted :) just one more basic question, is it possible to increase/decrease the image size or the image sizes are standard.

Note