在 FPDF 上一起解析 Html 和 Javascript
Parse Html and Javascript together on FPDF
我想使用 html(使用 php)和 运行 javascript 生成 PDF。我遇到了 FPDF,并且我分别通过库实现了这两个目标。
对于html,html parser:
require('WriteHTML.php');
$pdf=new PDF_HTML();
$pdf->AddPage();
$pdf->SetFont('Arial');
$pdf->WriteHTML('You can<br><p align="center">center a line</p>and add a horizontal rule:<br><hr><button>LaLaLa</button>');
$pdf->Output();
?>
这行得通。 (除了 <button></button>
标签;实际上 <a href="test.php">Test<a>
有效)
对于javascript,this example:
require('pdf_js.php');
class PDF_AutoPrint extends PDF_JavaScript {
function AutoPrint($dialog=false) {
$param=($dialog ? 'true' : 'false');
$script="print($param);";
$this->IncludeJS($script);
}
}
$pdf=new PDF_AutoPrint();
$pdf->AddPage();
$pdf->SetFont('Arial','',20);
$pdf->Text(90, 50, 'Print me!');
$pdf->AutoPrint(true);
$pdf->Output();
?>
最初实例化 $pdf
时,两个示例都需要调用它们的 class - $pdf=new PDF_AutoPrint();
和 $pdf=new PDF_HTML();
。
我应该如何将 html 中的按钮与 javascript 操作结合起来并在 FPDF 中使用?
编辑:使用它只会给我带来一个输出,而不是两个(输出行为类似于 return)
$pdf=new PDF_HTML();
$pdf->AddPage();
$pdf->SetFont('Arial');
$pdf->WriteHTML('You can<br><p align="center">center a line</p>and add a horizontal rule:<br><hr><a href="lala.php">jfdskjfjksd</a>');
$pdf2=new PDF_AutoPrint();
$pdf2->AddPage();
$pdf2->SetFont('Arial','',20);
$pdf2->Text(90, 50, 'Print me!');
//Open the print dialog
$pdf2->AutoPrint(true);
$pdf->Output();
$pdf2->Output();
在这种特殊情况下,由于 PDF_JavaScript
class 的源代码太小,您可以将这些方法添加到 PDF_AutoPrint
class 中,这将扩展 PDF_HTML
.
class PDF_AutoPrint extends PDF_HTML{
function AutoPrint($dialog=false) {
$param=($dialog ? 'true' : 'false');
$script="print($param);";
$this->IncludeJS($script);
}
function IncludeJS($script) {
$this->javascript=$script;
}
function _putjavascript() {
$this->_newobj();
$this->n_js=$this->n;
$this->_out('<<');
$this->_out('/Names [(EmbeddedJS) '.($this->n+1).' 0 R]');
$this->_out('>>');
$this->_out('endobj');
$this->_newobj();
$this->_out('<<');
$this->_out('/S /JavaScript');
$this->_out('/JS '.$this->_textstring($this->javascript));
$this->_out('>>');
$this->_out('endobj');
}
function _putresources() {
parent::_putresources();
if (!empty($this->javascript)) {
$this->_putjavascript();
}
}
function _putcatalog() {
parent::_putcatalog();
if (!empty($this->javascript)) {
$this->_out('/Names <</JavaScript '.($this->n_js).' 0 R>>');
}
}
}
注意:还要检查 PDF_JavaScript
的许可证,以确保允许这样的事情。
注意: 如果您使用的是 php >= 5.4.0,最好使用 Traits[=16= 来实现这样的功能]
我想使用 html(使用 php)和 运行 javascript 生成 PDF。我遇到了 FPDF,并且我分别通过库实现了这两个目标。
对于html,html parser:
require('WriteHTML.php');
$pdf=new PDF_HTML();
$pdf->AddPage();
$pdf->SetFont('Arial');
$pdf->WriteHTML('You can<br><p align="center">center a line</p>and add a horizontal rule:<br><hr><button>LaLaLa</button>');
$pdf->Output();
?>
这行得通。 (除了 <button></button>
标签;实际上 <a href="test.php">Test<a>
有效)
对于javascript,this example:
require('pdf_js.php');
class PDF_AutoPrint extends PDF_JavaScript {
function AutoPrint($dialog=false) {
$param=($dialog ? 'true' : 'false');
$script="print($param);";
$this->IncludeJS($script);
}
}
$pdf=new PDF_AutoPrint();
$pdf->AddPage();
$pdf->SetFont('Arial','',20);
$pdf->Text(90, 50, 'Print me!');
$pdf->AutoPrint(true);
$pdf->Output();
?>
最初实例化 $pdf
时,两个示例都需要调用它们的 class - $pdf=new PDF_AutoPrint();
和 $pdf=new PDF_HTML();
。
我应该如何将 html 中的按钮与 javascript 操作结合起来并在 FPDF 中使用?
编辑:使用它只会给我带来一个输出,而不是两个(输出行为类似于 return)
$pdf=new PDF_HTML();
$pdf->AddPage();
$pdf->SetFont('Arial');
$pdf->WriteHTML('You can<br><p align="center">center a line</p>and add a horizontal rule:<br><hr><a href="lala.php">jfdskjfjksd</a>');
$pdf2=new PDF_AutoPrint();
$pdf2->AddPage();
$pdf2->SetFont('Arial','',20);
$pdf2->Text(90, 50, 'Print me!');
//Open the print dialog
$pdf2->AutoPrint(true);
$pdf->Output();
$pdf2->Output();
在这种特殊情况下,由于 PDF_JavaScript
class 的源代码太小,您可以将这些方法添加到 PDF_AutoPrint
class 中,这将扩展 PDF_HTML
.
class PDF_AutoPrint extends PDF_HTML{
function AutoPrint($dialog=false) {
$param=($dialog ? 'true' : 'false');
$script="print($param);";
$this->IncludeJS($script);
}
function IncludeJS($script) {
$this->javascript=$script;
}
function _putjavascript() {
$this->_newobj();
$this->n_js=$this->n;
$this->_out('<<');
$this->_out('/Names [(EmbeddedJS) '.($this->n+1).' 0 R]');
$this->_out('>>');
$this->_out('endobj');
$this->_newobj();
$this->_out('<<');
$this->_out('/S /JavaScript');
$this->_out('/JS '.$this->_textstring($this->javascript));
$this->_out('>>');
$this->_out('endobj');
}
function _putresources() {
parent::_putresources();
if (!empty($this->javascript)) {
$this->_putjavascript();
}
}
function _putcatalog() {
parent::_putcatalog();
if (!empty($this->javascript)) {
$this->_out('/Names <</JavaScript '.($this->n_js).' 0 R>>');
}
}
}
注意:还要检查 PDF_JavaScript
的许可证,以确保允许这样的事情。
注意: 如果您使用的是 php >= 5.4.0,最好使用 Traits[=16= 来实现这样的功能]