TCPDF中的图像和文本格式

时间:2014-11-27 18:19:06

标签: php pdf-generation tcpdf

使用TCPDF以PDF格式生成HTML。但文本格式化不会出现,图像不存在于PDF中。 问题是什么?

PHP代码:

require_once('tcpdf/tcpdf.php');

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

// set document information
$pdf->SetCreator(PDF_CREATOR);
// set default header data
// $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 021', 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('helvetica', '', 9);

// add a page
$pdf->AddPage();
// create some HTML content

/*---------*/
$html  = 'HTML Content Which is pasted under the HTML head in this Question'; 

// output the HTML content
$pdf->writeHTML($html, true, 0, true, 0);

// reset pointer to the last page
$pdf->lastPage();
ob_end_clean();
//Close and output PDF document
$pdf->Output('rec_users.pdf','D');

HTML

<table>
        <tbody>
            <tr>
                <td style='background:red'>&nbsp;</td>

                <td><img alt='' height='129' src=
                'demo-300x300.jpg'
                width='300'></td>

                <td>
                    <div class='header'>
                        <div class='right'>
                            <p>1790 lee Travino</p>

                            <p>CL paso xyz</p>

                            <p>T-123456789</p>

                            <p>F-45678903</p>

                            <p>wwww.xyz.com</p>
                        </div>
                    </div>
                </td>
            </tr>

            <tr>
                <td style='background:red'>&nbsp;</td>

                <td>
                    <div class='letter_section'>
                        <h1>Service Agreement</h1>

                        <p style=
                        'color: #5d605f; font-family: arial; font-size: 20px; margin: 40px 0;'>
                        December 30,2013</p>

                        <h3 class='address'>Ms.jenifer Carter</h3>

                        <h3 class='address'>Ms.jenifer Carter</h3>

                        <h3 class='address'>Ms.jenifer Carter</h3>

                        <h3 class='address'>Ms.jenifer Carter</h3>

                        <h3 class='address'>Ms.jenifer Carter</h3>
                    </div>
                </td>

                <td>&nbsp;</td>
            </tr>

            <tr>
                <td style='background:red'>&nbsp;</td>

                <td colspan='2'>
                    <p>lorem ipum is the dolor sit amet lorem ipum is the dolor
                    sit amet lorem ipum is the dolor sit ametlorem ipum is the
                    dolor sit ametlorem ipum is the dolor sit amet lorem ipum
                    is the dolor sit amet lorem ipum is the dolor sit amet</p>

                    <p>lorem ipum is the dolor sit amet lorem ipum is the dolor
                    sit amet lorem ipum is the dolor sit ametlorem ipum is the
                    dolor sit ametlorem ipum is the dolor sit amet lorem ipum
                    is the dolor sit ametlorem ipum is the dolor sit amet lorem
                    ipum is the dolor sit amet lorem ipum is the dolor sit
                    ametlorem ipum is the dolor sit ametlorem ipum is the dolor
                    sit amet lorem ipum is the dolor sit amet</p>
                </td>
            </tr>
        </tbody>
    </table>

需要输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

1)检查图像路径..确认给定路径内的图像。 2)要打印图像,请参阅以下代码

$ PDF-&GT;图像($ IMAGE_PATH&#39; imageName.jpg&#39;,$ x_im + 32.8,$ y_im + 32,$ w_im,$ h_im);