HTML到PDF转换

时间:2015-03-04 09:49:11

标签: php html css pdf tcpdf

我想将HTML转换为PDF,我正在使用TCPDF。它正在正确转换,但html的大小没有变化。

$htmlcode .= '
    <form action="" method="POST">                  
                    <table width="1000" height="1000" style="background-color: #b91781;font-family: "freesansi"; color: #ffffff;font-size: 20px;">
                    <tbody><tr>
                      <td width="249" valign="middle" align="center">
                      <div style="height: 595; width: 249; text-align: left;padding-top:140px;">
                      <img src="../../../uploads/photo_1425451614.jpg" style="border-radius: 159px;
                        width: 159px; height:225px;margin:70px auto 0;display:block;">
                        </div>
                      </td>
                      <td width="583" valign="middle" align="center">
                      <div style="height: 188px; width: 448px; text-align: left;">
                        <span style="display: block;
                        float: left;">To <span style="font-size: 22px; font-weight: 700; line-height: 34px; position: relative; top: 17px;">gdffsd</span>
                      </span><br><br>
                    <div style="clear: both; display: inline-block; min-height: 160px; padding: 0px 18px; margin: 28px auto 0px;color: #ffffff">From the day you were born,<br>
                    I fell in love with you my girl.<br>
                    And when you bloomed like a rose without a single thorn,<br>
                    You turned my entire life into a most precious pearl.<br>
                    Your love and care for me is a beautiful dawn,<br>
                    That marks the end of darkness left to unfurl.<br><br>

                    Feeling blessed to have you as my daughter. </div>
                    <br><br>
                    <span style="display: block;
                         float: right;">From<span style="font-size: 22px; font-weight: 700; line-height: 34px; position: relative; top: 18px;">fsdffsd</span> 
                      </span></div>
                      </td>
                      </tr>
                    </tbody></table>

                    </form>
                ';

PDF屏幕截图:enter image description here

正如大家都看到中心框高度应该增加但不起作用。每次产生相同的输出。我应该在代码中更改什么?

TCPDF代码:

require_once('tcpdf_include.php');


    class MYPDF extends TCPDF {

        public function Header() {
            // Logo
            //$image_file = base_url.'images/logo.png';
            $this->Image($image_file, 15, 5, 40, '20', 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false);
            // Set font
            $this->SetFont('helvetica', 'B', 20);
            // Title
            //$this->Cell(0, 15, 'At Need Plan', 0, false, 'C', 0, '', 0, false, 'M', 'M');
        }

        // Page footer
        public function Footer() {
            // Position at 15 mm from bottom
            $this->SetY(-15);
            // Set font
            $this->SetFont('helvetica', 'I', 8);
            // Page number
            $this->Cell(0, 10, 'All Rights Reserved', 0, false, 'C', 0, '', 0, false, 'T', 'M');
        }
    }

    // create new PDF document
    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, array(800, 800), true, 'UTF-8', false);

    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Schwarzkopf');
    $pdf->SetTitle('Category');
    $pdf->SetSubject('Category');
    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

    // set default header data
    $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

    // set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

    // set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

    // set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

    // set image scale factor
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

    // set some language-dependent strings (optional)
    if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
        require_once(dirname(__FILE__).'/lang/eng.php');
        $pdf->setLanguageArray($l);
    }

    // ---------------------------------------------------------

    // set font
    $pdf->SetFont('freesansi', 'BI', 12);

    // add a page
    $pdf->AddPage();

    //$pdf->Write(0, 'Example of HTML tables', '', 0, 'L', true, 0, false, false, 0);

    $pdf->SetFont('freesansi', '', 11);


    $htmlcode = $htmlcode;



    $pdf->writeHTML($htmlcode, true, false, false, false, '');



    // -----------------------------------------------------------------------------

    //Close and output PDF document
    $path = $_SERVER['DOCUMENT_ROOT'].folder_name.'html2_image';

    $filename = $_SERVER['DOCUMENT_ROOT'].folder_name."html2_image/";

    $path1 = $_SERVER['DOCUMENT_ROOT'].folder_name."html2_image";

    if (!file_exists($filename)) {
        mkdir($path1, 0777);        
    }

    $unique = rand().time();

    $pdfname = 'my.pdf';

    $pdf->Output($path.'/'.$pdfname, 'F');

0 个答案:

没有答案