我正在尋找一個最初的PDF文件,一個有圖形和文本,然後採取一些HTML代碼,其中有一些用戶輸入的動態值,生成一個PDF格式,希望或者使用最初的PDF作爲背景,或者以某種方式運行PHP腳本,然後將兩個PDF作爲另一個背景的「合併」。如何在現有PDF上疊加HTML生成的PDF?
我有一些代碼,生成一個HTML格式的PDF:(使用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。
我也有其他的PHP代碼由具有PDF作爲初始源呈現一個PDF:(使用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」,並使用它作爲背景,寫一些有價值的文件,並保存新生成的文件。
雖然這基本上是我想要做的,但我需要使用html,因爲文本的確切格式變得嚴格,幾乎不可能通過手動繪製文本來完成。
我打算使用DOMPDF,FPDF,FPDI,TCPDF或任何其他PHP資源來完成此操作。
有沒有辦法融合上述兩種方式?
我推薦一個基於會話的文件名。當用戶提交表單時,他/她在服務器上創建一個PDF文件。文件名將是用戶會話。使用[scandir()](php.net/manual/en/function.scandir.php)或file_exists()檢查該特定用戶是否有任何PDF,並顯示所有的pdf文件;如果具有該用戶會話的文件存在,請使用另一個提交按鈕將文件名轉換爲第二個代碼。 –
@MawiaHL這不是我的問題的根源,它已經保存爲基於會話的文件名,並帶有動態文件名加載項。問題是在另一個PDF上渲染一個PDF。意思是1頁+ 1頁= 1頁的方式,而不是單個渲染。 – NoReceipt4Panda