如何将HTML生成的PDF覆盖在现有PDF之上? [英] How to overlay HTML generated PDF on top of existing PDF?

查看:213
本文介绍了如何将HTML生成的PDF覆盖在现有PDF之上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从一个初始的PDF文件开始,该文件具有图形和文本,然后使用一些对某些用户输入具有动态值的html代码,将其生成为PDF,希望将初始PDF用作背景,或者以某种方式随后运行PHP脚本来合并"两个PDF,其中一个充当另一个背景.

I'm looking to start with an initial PDF file, one that has graphics and text, and then take some html code which has dynamic values for some user input, generate that as a PDF, hopefully either using the initial PDF as a background, OR somehow running a PHP script afterwards to "merge" both PDF where one acts as a background to another.

我有一些代码可以呈现HTML格式的PDF :(使用DOMPDF)

I have some code that renders an HTML formatted PDF: (using DOMPDF)

$initialpdf = file_get_contents('file_html.html');

$initialpdf = str_replace(array(
        '%replaceText1%',
        '%replaceText2%'
    ), array (
        $replaceText1,
        $replaceText2,
    ), $initialpdf);

$fp = fopen('file_html_new.html','w');
file_put_contents('file_html_new.html', $initialpdf);

require_once("dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
function pdf_create($html, $filename, $paper, $orientation, $stream=TRUE)
{
    $dompdf = new DOMPDF();
    $dompdf->set_paper($paper,$orientation);
    $dompdf->load_html($html);
    $dompdf->render();
    $pdf = $dompdf->output();
    @file_put_contents($filename . ".pdf", $pdf);
}
$filename = 'HTML_Generated_pdf';
$dompdf = new DOMPDF();
$html = file_get_contents('file_html_new.html'); 
pdf_create($html,$filename,'Letter','landscape');

上面的代码采用html文件"file_html.html",并用用户输入值替换字符串,将其呈现为名为"file_html_new.html"的新HTML文件,然后将其呈现为PDF.

The code above takes html file "file_html.html" and does string replacements with user input values, renders this as a new HTML file called "file_html_new.html" and then renders that AS a PDF.

我还具有其他通过将PDF作为初始来源来呈现PDF的PHP代码:(使用FPDF)

I also have other PHP code that render a PDF by having a PDF as an initial source: (using FPDF)

<?php
ob_clean();
ini_set("session.auto_start", 0);
define('FPDF_FONTPATH','font/');
define('FPDI_FONTPATH','font/');
require('fpdf.php');
require('fpdi.php');

$pdf = new FPDI();
$pdf->setSourceFile("/home/user/public_html/wp-content/myPDF.pdf");
$tplIdx = $pdf->importPage(1);
$specs = $pdf->getTemplateSize($tplIdx);
$pdf->addPage($specs['h'] > $specs['w'] ? 'P' : 'L', 'Letter');
$pdf->useTemplate($tplIdx,  0, 0);

$pdf->SetFont('helvetica');
$pdf->SetXY(30, 30);
$pdf->Write(0, $replaceText1);
ob_end_clean();
$pdf->Output('New_Generated_PDF.pdf', 'F');
?>

这将获取一个已经存在的PDF,即"myPDF.pdf",并将其用作背景,将一些传递的值写入文档中,并保存新生成的文档.

This takes an already existing PDF, "myPDF.pdf", and uses it as a background, writing some passed in value to the document, and saving the newly produced document.

虽然这基本上是我想要做的,但我需要使用html,因为文本的准确格式变得严格,几乎不可能仅通过手动将其绘制出来.

While this is essentially what I want to do, I need to work with html because the exact formatting for text gets rigorous and almost impossible to do just by plotting it in manually.

为了实现这一目标,我愿意使用DOMPDF,FPDF,FPDI,TCPDF或任何其他PHP资源.

I'm open to using DOMPDF, FPDF, FPDI, TCPDF, or any other PHP resource in order to accomplish this.

有没有办法融合我上面的两种方式?

Is there a way to fuse the two ways I have above?

推荐答案

请确保您也可以将其他现有PDF文档与FPDI一起使用.这段代码应该向您展示这个概念(实际上我想所有的页面格式都是A4纵向):

For sure you can use different existing PDF documents with FPDI, too. This code should show you the concept (actually I guess that all page formats are A4 portrait):

<?php

$pdf = new FPDI();

// let's get an id for the background template
$pdf->setSourceFile('myPDF.pdf'); 
$backId = $pdf->importPage(1);

// iterate over all pages of HTML_Generated_pdf.pdf and import them
$pageCount = $pdf->setSourceFile('HTML_Generated_pdf.pdf');
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
    // add a page
    $pdf->AddPage();
    // add the background
    $pdf->useTemplate($backId);
    // import the content page
    $pageId = $pdf->importPage($pageNo);
    // add it
    $pdf->useTemplate($pageId);
}

$pdf->Output();

这篇关于如何将HTML生成的PDF覆盖在现有PDF之上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆