[英]Adding input fields in HTML form for converting to PDF
我正在使用此FPDF HTML到PDF转换脚本 。
我需要添加更多输入字段以包含在生成的pdf中。 在表单中,我尝试添加另一个名称为html2的字段,如下所示:
Text 2: <input type="text" name="html2">
并在php部分中进行了如下更改:
$_POST['html2']
在html2pdf.php文件中,以下功能:
function __construct($_html,$_title,$_author,$_date) {
$this->html=$_html;
更改为:
function __construct($_html,$_title,$_author,$_date,$_html2)
$this->html=$_html;
$this->html2=$_html2;
但是输入类型'html2'不会显示在生成的最终pdf中。 一切正常。 我需要在html2pdf.php文件中进行哪些其他更改?
我的表单文件的代码如下:
<?php
require('fpdf/html2pdf.php');
if(isset($_POST['title']))
{
$pdf = new createPDF(
$_POST['html'],
$_POST['title'],
$_POST['author'],
time(),
$_POST['html2']
);
$pdf->run();
}
?>
<form name="pdfgen" method="post" target="_blank" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Title: <input type="text" name="title">
Author: <input type="text" name="author">
Text 1: <input type="text" name="html">
Text 2: <input type="text" name="html2">
<input type="submit" value="Generate PDF">
</form>
这似乎是一个基本的编码问题,但是要回答这个问题,我将需要很长的答案。 首先,将HTML转换为PDF转换脚本。 用该文件替换该文件中的createPDF()
类。
/************************************/
/* main class createPDF */
/************************************/
class createPDF {
function __construct($_title,$_articleurl,$_author,$_date) {
// main vars
$this->title=$_title; // article title
$this->articleurl=$_articleurl; // article URL
$this->author=$_author; // article author
$this->date=$_date; // date being published
// other options
$this->from='iso-8859-2'; // input encoding
$this->to='cp1250'; // output encoding
$this->useiconv=false; // use iconv
$this->bi=true; // support bold and italic tags
}
function _convert($s) {
if ($this->useiconv)
return iconv($this->from,$this->to,$s);
else
return $s;
}
function start() {
$pdf=new PDF('P','mm','A4',$this->title,$this->articleurl,false);
$pdf->SetCreator("Script by Radek HULAN, http://hulan.info/blog/");
$pdf->SetDisplayMode('real');
$pdf->SetTitle($this->_convert($this->title));
$pdf->SetAuthor($this->author);
$pdf->AddPage();
// header
$pdf->PutMainTitle($this->_convert($this->title));
$pdf->PutMinorHeading('Article URL');
$pdf->PutMinorTitle($this->articleurl,$this->articleurl);
$pdf->PutMinorHeading('Author');
$pdf->PutMinorTitle($this->_convert($this->author));
$pdf->PutMinorHeading("Published: ".@date("F j, Y, g:i a",$this->date));
$pdf->PutLine();
$pdf->Ln(10);
// store pdf in this class
$this->fpdf = $pdf;
}
function insertHTML($html) {
// change some win codes, and xhtml into html
$str=array(
'<br />' => '<br>',
'<hr />' => '<hr>',
'[r]' => '<red>',
'[/r]' => '</red>',
'[l]' => '<blue>',
'[/l]' => '</blue>',
'“' => '"',
'”' => '"',
'„' => '"',
'…' => '...',
'’' => '\''
);
foreach ($str as $_from => $_to) $html = str_replace($_from,$_to,$html);
// html
$this->fpdf->WriteHTML($this->_convert(stripslashes($html)),$this->bi);
}
function finish() {
// output
$this->fpdf->Output();
// stop processing
exit;
}
function newPage() {
$this->fpdf->AddPage();
}
}
请注意,毕竟您要转换多个HTML部分,而不只是一个,所以我从构造函数中删除了$_html
参数。 我将run()
方法分为三个部分: start()
, insertHTML()
和finish()
。 我已经将$html
参数添加到insertHTML()
方法中。 这样,您可以将HTML的多个部分插入PDF。 我还添加了newPage()
方法,因为您可能希望将HTML的第二部分放在新页面上。
现在为您自己的代码。 您需要将此更改为:
<?php
require('fpdf/html2pdf.php');
if(isset($_POST['title']))
{
$pdf = new createPDF(
$_POST['title'],
$_POST['author'],
time()
);
$pdf->start();
$pdf->insertHTML($_POST['html']);
$pdf->newPage();
$pdf->insertHTML($_POST['html2']);
$pdf->finish();
}
?>
<form name="pdfgen" method="post" target="_blank" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Title: <input type="text" name="title">
Author: <input type="text" name="author">
Text 1: <input type="text" name="html">
Text 2: <input type="text" name="html2">
<input type="submit" value="Generate PDF">
</form>
如您所见,我首先创建PDF对象,然后开始第一页,插入HTML的第一部分,请求新页面,插入HTML的第二部分,最后创建输出。 因此,它并不那么复杂,只是很多代码。 希望这能使您再次尝试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.