使用 PHP 创建 PDF 文件 [英] Create a PDF file with PHP
本文介绍了使用 PHP 创建 PDF 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从我用 php 编写的网页创建 pdf 文件.我的文档必须从mysql生成并生成pdf文件.我想保存这个pdf并阅读.请给我代码示例.
I want to create pdf file from my web page written by php . My document must produce from mysql and produce pdf file.I want to be to save this pdf and to read.Please give me code sample.
推荐答案
使用 TCPD 库:
<?
require_once("tcpdf/tcpdf.php");
$pdf = new TCPDF();
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("my name");
$pdf->SetTitle("my doc");
$pdf->SetSubject("x");
$pdf->SetKeywords("a, b, c");
// set default header data
$pic = "/gfx/header.png";
$pdf->SetHeaderData(realpath($pic), "25", "Title");
// 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 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
$pdf->setLanguageArray($l);
//initialize document
$pdf->AliasNbPages();
// add a page
$pdf->AddPage();
// ---------------------------------------------------------
// set font
$pdf->SetFont("helvetica", "", 12);
// set a background color
$pdf->SetFillColor(230, 240, 255, true);
$pdf->SetFont("", "b", 16);
$pdf->Write(16, "some text
", "", 0, 'C');
$pdf->Output("filename.pdf", "I");
?>
这篇关于使用 PHP 创建 PDF 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文