我使用了几种搜索和方法来找到从 html 到 pdf 的转换,但我没有。将HTML页面转换为PDF的方法是什么?我已经下载tcpdf library
了,但我不知道如何使用它。
提前致谢。
我使用了几种搜索和方法来找到从 html 到 pdf 的转换,但我没有。将HTML页面转换为PDF的方法是什么?我已经下载tcpdf library
了,但我不知道如何使用它。
提前致谢。
请使用tcpdf,尝试tcpdf示例链接http://www.tcpdf.org/examples.php中的示例 6
试试这个例子:
require_once('write path to include /config/lang/eng.php');
require_once('write path to include 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);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 006');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 006', 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 auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$pdf->setLanguageArray($l);
// ---------------------------------------------------------
// set font
$pdf->SetFont('dejavusans', '', 10);
// add a page
$pdf->AddPage();
// writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='')
// writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=true)
// create some HTML content
$html = 'Write your html code here';
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
$this->pdf->lastPage();
ob_clean();
//Close and output PDF document
$this->pdf->Output($filename, 'D'); //D if you want download it use I if you want open it on new tab
从tcpdf/config/tcpdf_config.php更改常量值,例如PDF_HEADER_LOGO
使用来自htm2pdf.co.uk的简单或高级 API
使用 TCPDF
请下载 tcpdf 并将其提取并放入您的应用程序文件夹中
在需要转换pdf的页面中,请将需要在pdf中显示的html部分添加到变量中
例如:$data = "Hello World"; /在此处包含 tcpdf /
$tcpdf = new TCPDF();
$textfont = 'helvetica';
$tcpdf->SetAuthor("Julia Holland");
$tcpdf->SetAutoPageBreak(true);
$tcpdf->setPrintHeader(false);
$tcpdf->setPrintFooter(false);
$tcpdf->SetTextColor(0, 0, 0);
$tcpdf->SetFont($textfont,'',9);
$tcpdf->AddPage();
$tcpdf->writeHTML($data, true, 0, true, 0);
$tcpdf->Output('filename11.pdf', 'I');
现在您可以使用 tcpdf 站点中列出的自定义字体、样式蚀刻,并且这个以 pdf 格式打印 Hello world
从这里下载 tcpdf http://www.tcpdf.org/
按照这个链接http://www.tcpdf.org/ 试试这个
<?php
// Include the main TCPDF library (search for installation path).
require_once('../lib/tcpdf/tcpdf.php');
// extend TCPF with custom functions
class MYPDF extends TCPDF {
// Colored table
public function ColoredTable($header,$data) {
// Colors, line width and bold font
$this->SetFillColor(255, 0, 0);
$this->SetTextColor(255);
$this->SetDrawColor(128, 0, 0);
$this->SetLineWidth(0.3);
$this->SetFont('', 'B');
// Header
$w = array(40, 35, 40, 45);
$num_headers = count($header);
for($i = 0; $i < $num_headers; ++$i) {
$this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
}
$this->Ln();
// Color and font restoration
$this->SetFillColor(224, 235, 255);
$this->SetTextColor(0);
$this->SetFont('');
// Data
$fill = 0;
foreach($data as $row) {
$this->Cell($w[0], 6, $row[0], 'c');
$this->Cell($w[1], 6, $row[1], 'c');
$this->Cell($w[2], 6, number_format($row[2]), 'LR', 0, 'R', $fill);
$this->Cell($w[3], 6, number_format($row[3]), 'LR', 0, 'R', $fill);
$this->Ln();
$fill=!$fill;
}
$this->Cell(array_sum($w), 0, '', 'T');
}
}
// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 011');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 011', 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)
$lg = Array();
$lg['a_meta_charset'] = 'UTF-8';
$lg['a_meta_dir'] = 'rtl';
$lg['a_meta_language'] = 'fa';
$lg['w_page'] = 'page';
// set some language-dependent strings (optional)
$pdf->setLanguageArray($lg);
// ---------------------------------------------------------
// set font
$pdf->SetFont('aealarabiya', '', 18);
// add a page
$pdf->AddPage();
// column titles
$header = array('Country', 'Capital', 'Area (sq km)', 'Pop. (thousands)');
// data loading
$data = array('data1' , 'data2' , '323' , '243');
// print colored table
$pdf->ColoredTable($header, $data);
// ---------------------------------------------------------
// close and output PDF document
ob_end_clean();
$pdf->Output('example_011.pdf', 'I');
?>