【问题标题】:Use different image from two PDF for create one PDF with FPDI/TCPDF使用来自两个 PDF 的不同图像创建一个带有 FPDI/TCPD 的 PDF
【发布时间】:2020-10-26 11:38:17
【问题描述】:

我会使用来自两个不同 PDF 的两个不同页面来创建一个 PDF 并结合两个页面。 为此,我使用 FPDF,并且已经这样做了:

$finalPDF  = new Fpdi();
$secondPDF = new Fpdi();

$personalizationPdfPath   = "Path_of_my_first_PDF";
$templatePath             = "Path_of_my_second_PDF";

$finalPDF->setSourceFile($templatePath);
$secondPDF->setSourceFile($personalizationPdfPath);

// Import the first page
$page1 = $pdfFinal->importPage(1, PdfReader\PageBoundaries::MEDIA_BOX);

// Import the second page of second PDF
$page2 = $secondPDF->importPage(2, PdfReader\PageBoundaries::MEDIA_BOX);


// Get the size
$dimension = $finalPDF->getTemplateSize($template_page);

// Add the page
$finalPDF->AddPage($dimension["orientation"], array($dimension["width"], $dimension["height"]));

// Apply the page1 on the finalPDF
$finalPDF->useTemplate($page1, 0, 0, $dimension["width"], $dimension["height"]);

// Apply the page2 on the finalPDF
$finalPDF->useTemplate($page2, 20, 28, $dimension["width"]*0.75, $dimension["height"]*0.75); //error

但是当我运行它时,我有模板不存在错误。如果我把 $page1 而不是 $page2 它工作,两个页面是合并的。第一个 100% 大小和第二个 75% 大小。 我不知道为什么 $page2 不起作用。我已经使用 dd(dump die) 来查看两个 $pages 之间的区别,没什么相关的。

所以我使用了另一种方法,将 $page2 转换为图片并使用 AddImage 方法:

$imageFromPDF = "Path_of_my_image.jpg";
$finalPdf->Image($imageFromPDF, 35, 35, $dimension["width"]*0.70, $dimension["height"]*0.70, "JPG");


$pdfFinal->Output("F", "nameOfPdf");

效果很好,但质量很差。我已阅读此subject,但质量仍然很垃圾。

在这两种方式中,有人有好的解决方案吗? 谢谢

【问题讨论】:

    标签: fpdf tcpdf fpdi image-quality


    【解决方案1】:

    一步一步来。不需要 2 个 FPDI 实例。

    $pdf = new Fpdi();
    
    // set the first document as the source
    $pdf->setSourceFile($templatePath);
    // then import the first page of it
    $page1 = $pdf->importPage(1, PdfReader\PageBoundaries::MEDIA_BOX);
    
    // now set the second document as the source
    $pdf->setSourceFile($personalizationPdfPath);
    // and import the second page of it:
    $page2 = $pdf->importPage(2, PdfReader\PageBoundaries::MEDIA_BOX);
    
    // to get the size of an imported page, you need to pass the 
    // value returned by importPage() and not an undefined variable 
    // such as $template_page!!
    $dimensions = $pdf->getTemplateSize($page1); // or $page2?
    
    // Add a page
    $pdf->AddPage($dimension["orientation"], $dimension);
    
    // ...now use the imported pages as you want...
    // Apply the page1 on the finalPDF
    $pdf->useTemplate($page1, 0, 0, $dimension["width"], $dimension["height"]);
    
    // Apply the page2 on the finalPDF
    $pdf->useTemplate($page2, 20, 28, $dimension["width"] * 0.75, $dimension["height"] * 0.75);
    

    2028 的值对我来说看起来很奇怪,但这是您使用的值。

    【讨论】:

      猜你喜欢
      • 2011-10-04
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-11
      • 1970-01-01
      相关资源
      最近更新 更多