简体   繁体   中英

Adding input fields in HTML form for converting to PDF

I am using this FPDF HTML to PDF Conversion script .

I need to add more input fields to be included in the pdf that is generated. In the form I tried adding another field with the name html2 like this:

Text 2: <input type="text" name="html2">

and made the following changes in the php part like this:

   $_POST['html2']  

In the html2pdf.php file the following function:

function __construct($_html,$_title,$_author,$_date) {
    $this->html=$_html;

was changed to:

function __construct($_html,$_title,$_author,$_date,$_html2)
        $this->html=$_html;  
        $this->html2=$_html2; 

But input type 'html2' does not show up in the final pdf that is generated. Everything else works ok. What other changes do I need to make in the html2pdf.php file?

The code for my form file is as under:

<?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>

This seems like quite a basic coding problem, but to answer it I will need quite a long answer. First the changes to the HTML to PDF Conversion script. Replace the createPDF() class, in that file, with this one.

/************************************/
/* 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>',
        '&#8220;' => '"',
        '&#8221;' => '"',
        '&#8222;' => '"',
        '&#8230;' => '...',
        '&#8217;' => '\''
        );
        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();
    }

} 

Notice that I removed the $_html argument from the constructor, after all you want to convert multiple HTML sections, not just one. I've split the run() method into three parts: start() , insertHTML() and finish() . I've added the $html argument to the insertHTML() method. This way you can insert multiple sections of HTML into the PDF. I've also added a newPage() method, because you might want to put the second section of HTML on a new page.

Now for your own code. You need to change that to this:

<?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>

As you can see I first create the PDF objects, then I start the first page, insert the first section of HTML, request a new page, insert the second section of HTML, and finally I create the output. So, it's not that complex, it is just a lot of code. I hope this gets you going again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM