Herramientas de usuario

Herramientas del sitio


informatica:lenguajes_de_programacion:php:php_pdf

PHP FPDF

PHP FPDI

iniciar FPDI sin composer: https://stackoverflow.com/questions/47292751/fpdf-fpdi-error-fatal-error-class-setasign-fpdi-fpdftpl-not-found-in

https://stackoverflow.com/questions/46792713/php-class-fpdi-not-found-but-the-file-defining-it-is-included

require_once("libs/fpdf182/fpdf.php");
require_once("libs/FPDI-master/src/autoload.php");
 
$pdf = new \setasign\Fpdi\Fpdi();

Juntar dos pdf en un solo pdf

https://stackoverflow.com/questions/28744300/fpdf-mergin-2-pdf-files-into-1-pdf-with-1-page

require_once('fpdf.php');
require_once('fpdi.php');
 
$pdf = new FPDI();
 
$pdf->setSourceFile("Fantastic-Speaker.pdf");
$tplIdxA = $pdf->importPage(1, '/MediaBox');
 
$pdf->setSourceFile("Another-Fantastic-Speaker.pdf");
$tplIdxB = $pdf->importPage(1, '/MediaBox');
 
$pdf->addPage();
// place the imported page of the first document:
$pdf->useTemplate($tplIdxA, 10, 10, 90);
// place the imported page of the snd document:
$pdf->useTemplate($tplIdxB, 100, 10, 90);
 
$pdf->Output();

Ejemplos:

$pdf = new \setasign\Fpdi\Fpdi();
 
//En pageCount se guarda la cantidad de hojas.
$pageCount = $pdf->setSourceFile("facturas/202010/121_3341_20201019-0.pdf");

Importa y Rota segun el caso https://manuals.setasign.com/fpdi-manual/v1/the-fpdi-class/examples/

<?php
require_once('fpdf/fpdf.php');
require_once('fpdi/fpdi.php');
 
// initiate FPDI
$pdf = new FPDI();
 
// get the page count
$pageCount = $pdf->setSourceFile('Laboratory-Report.pdf');
// iterate through all pages
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
    // import a page
    $templateId = $pdf->importPage($pageNo);
    // get the size of the imported page
    $size = $pdf->getTemplateSize($templateId);
 
    // create a page (landscape or portrait depending on the imported page size)
    if ($size['w'] > $size['h']) {
        $pdf->AddPage('L', array($size['w'], $size['h']));
    } else {
        $pdf->AddPage('P', array($size['w'], $size['h']));
    }
 
    // use the imported page
    $pdf->useTemplate($templateId);
 
    $pdf->SetFont('Helvetica');
    $pdf->SetXY(5, 5);
    $pdf->Write(8, 'A complete document imported with FPDI');
}
 
// Output the new PDF
$pdf->Output();

problemas con FPDI version de comprension

Usando Ghostscript:

shell_exec( "gswin32 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=".$new_pdf." ".$old_pdf.""); 

Para solucionar el problema:

'This PDF document probably uses a compression technique which is not supported by the free parser shipped with FPDI. (See https://www.setasign.com/fpdi-pdf-parser for more details)
shell_exec( "gswin32 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=".$new_pdf." ".$old_pdf.""); 
$PDF = new Fpdi();
try {
    $PDF->setSourceFile('./pdf.pdf');
} catch (\Exception $exception) {
    if (aBoolFunctionToDetectThisParticularException($exception)) {
        exec('pdftk ./pdf.pdf output ./pdf_expanded.pdf uncompress');
        $PDF->setSourceFile('./pdf_expanded.pdf');
    } else {
        throw $exception;
    }
}

DOM PDF

GitHUB:

Cabecera y Pie de página:

Paginas:

require_once("dompdf/dompdf/autoload.inc.php");
use Dompdf\Dompdf;
 
class Pdfgenerator {
 
  public function generate($html, $filename='', $stream=TRUE, $paper = 'A4', $orientation = "portrait")
  {
    $dompdf = new DOMPDF();
    $dompdf->loadHtml($html);
    $dompdf->setPaper($paper, $orientation);
    $dompdf->render();
    if ($stream) {
        $dompdf->stream($filename.".pdf", array("Attachment" => 0));
    } else {
        return $dompdf->output();
    }
  }
}

Ejemplo usando ob_start() y ob_get_clean()

<?php
   ob_start();
?>
 
<table>
   <tbody>
       <tr>
           <td>abc</td>
           <td>abc</td>
           <td>abc</td>
           <td>abc</td>
       </tr>
   </tbody>
</table>
 
<?php
   $html = ob_get_clean();
   $pdfgenerator->generate($html, "Liquidacion.pdf");
?>
informatica/lenguajes_de_programacion/php/php_pdf.txt · Última modificación: 2025/04/08 00:49 por admin