直接將文件流內(nèi)容寫入文件,而文件的后綴名設(shè)為 pdf 格式就可以了;PHP 不管保存文件流還是文本,都是用這種方式;

創(chuàng)新互聯(lián)從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元千山做網(wǎng)站,已為上家服務(wù),為千山各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220
$path?=?"demo.pdf";
$ret??=?file_put_contents($path,?$arr['data']);
if?($ret)?{
echo?'保存成功';?//?然后可以取查看文件
}
同步
首先需要安裝wkhtmltopdf這個(gè)軟件 需要執(zhí)行shell_exec函數(shù)的php文件和安裝的wkhtmltopdf.exe可執(zhí)行文件在同一目錄下,不然php找不到這個(gè)執(zhí)行文件
shell_exec("wkhtmltopdf.exe --page-size A4 --outline 網(wǎng)頁(yè)地址 pdf文件名");
--page-size 以A4紙的尺寸顯示pdf內(nèi)容
--outline 顯示目錄(網(wǎng)頁(yè)中h1,h2來(lái)定)
網(wǎng)頁(yè)地址 需要轉(zhuǎn)成pdf的網(wǎng)頁(yè)地址
pdf文件名 生成pdf的文件名,可以使用絕對(duì)路徑
分享這個(gè)轉(zhuǎn)換成pdf得函數(shù)給你function pdf($html='h1 style="color:red"hello word/h1'){vendor('Tcpdf.tcpdf');$pdf = new \Tcpdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);// 設(shè)置打印模式$pdf-SetCreator(PDF_CREATOR);$pdf-SetAuthor('Nicola Asuni');$pdf-SetTitle('TCPDF Example 001');$pdf-SetSubject('TCPDF Tutorial');$pdf-SetKeywords('TCPDF, PDF, example, test, guide');// 是否顯示頁(yè)眉$pdf-setPrintHeader(false);// 設(shè)置頁(yè)眉顯示的內(nèi)容$pdf-SetHeaderData('logo.png', 60, 'baijunyao.com', '白俊遙博客', array(0,64,255), array(0,64,128));// 設(shè)置頁(yè)眉字體$pdf-setHeaderFont(Array('dejavusans', '', '12'));// 頁(yè)眉距離頂部的距離$pdf-SetHeaderMargin('5');// 是否顯示頁(yè)腳$pdf-setPrintFooter(true);// 設(shè)置頁(yè)腳顯示的內(nèi)容$pdf-setFooterData(array(0,64,0), array(0,64,128));// 設(shè)置頁(yè)腳的字體$pdf-setFooterFont(Array('dejavusans', '', '10'));// 設(shè)置頁(yè)腳距離底部的距離$pdf-SetFooterMargin('10');// 設(shè)置默認(rèn)等寬字體$pdf-SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);// 設(shè)置行高$pdf-setCellHeightRatio(1);// 設(shè)置左、上、右的間距$pdf-SetMargins('10', '10', '10');// 設(shè)置是否自動(dòng)分頁(yè) 距離底部多少距離時(shí)分頁(yè)$pdf-SetAutoPageBreak(TRUE, '15');// 設(shè)置圖像比例因子$pdf-setImageScale(PDF_IMAGE_SCALE_RATIO);if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {require_once(dirname(__FILE__).'/lang/eng.php');$pdf-setLanguageArray($l);}$pdf-setFontSubsetting(true);$pdf-AddPage();// 設(shè)置字體$pdf-SetFont('stsongstdlight', '', 14, '', true);$pdf-writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);$pdf-Output('example_001.pdf', 'I'); }
PHP也可以實(shí)現(xiàn)導(dǎo)出Word文檔為PDF的功能,不過(guò)要借助于第三方的類庫(kù),今天我們將為大家介紹PHP依靠com.sun.star.ServiceManager來(lái)轉(zhuǎn)換Word為PDF文檔的相關(guān)技巧。
PHP處理Word轉(zhuǎn)PDF的示例代碼:
02????set_time_limit(0);????
03????function?MakePropertyValue($name,$value,$osm){????
04????????$oStruct=$osm-Bridge_GetStruct("com.sun.star.beans.PropertyValue");????
05????????$oStruct-Name?=?$name;????
06????????$oStruct-Value?=?$value;????
07????return?$oStruct;????
08????}????
09????function?word2pdf($doc_url,?$output_url){????
10????????$osm?=?new?COM("com.sun.star.ServiceManager")or?die?("請(qǐng)確認(rèn)OpenOffice.org庫(kù)是否已經(jīng)安裝.\n");????
11????????$args?=?array(MakePropertyValue("Hidden",true,$osm));????
12????????$oDesktop?=?$osm-createInstance("com.sun.star.frame.Desktop");????
13????????$oWriterDoc?=?$oDesktop-loadComponentFromURL($doc_url,"_blank",?0,?$args);????
14????????$export_args?=?array(MakePropertyValue("FilterName","writer_pdf_Export",$osm));????
15????????$oWriterDoc-storeToURL($output_url,$export_args);????
16????????$oWriterDoc-close(true);????
17????}????
18????$output_dir?=?"D:/temp/";????
19????$doc_file?=?"D:/temps/test.doc";????
20????$pdf_file?=?"test.pdf";????
21????$output_file?=?$output_dir.$pdf_file;????
22????$doc_file?=?"".$doc_file;????
23????$output_file?=?"".$output_file;????
24????word2pdf($doc_file,$output_file);????
25?????
FPDF有提供表格和加入圖片等功能。
如果要使用PHP生成一個(gè)復(fù)雜的包括圖片和表格的PDF,還是需要手動(dòng)代碼來(lái)生成。沒(méi)有什么更好的辦法。 還是多看看演示,再結(jié)合一下就成了。
good luck
1首先下一個(gè)phpexcel
2下載完成的是一個(gè)壓縮文件,解壓放到你的項(xiàng)目目錄里
3.下面進(jìn)入代碼;
4.
//引入PHPExcel庫(kù)文件(路徑根據(jù)自己情況)
include './phpexcel/Classes/PHPExcel.php';
//創(chuàng)建對(duì)象
$excel = new PHPExcel();
//Excel表格式,這里簡(jiǎn)略寫了8列
$letter = array('A','B','C','D','E','F','F','G');
//表頭數(shù)組
$tableheader = array('學(xué)號(hào)','姓名','性別','年齡','班級(jí)');
//填充表頭信息
for($i = 0;$i count($tableheader);$i++) {
$excel-getActiveSheet()-setCellValue("$letter[$i]1","$tableheader[$i]");
}
5.
//表格數(shù)組
$data = array(
array('1','小王','男','20','100'),
array('2','小李','男','20','101'),
array('3','小張','女','20','102'),
array('4','小趙','女','20','103')
);
//填充表格信息
for ($i = 2;$i = count($data) + 1;$i++) {
$j = 0;
foreach ($data[$i - 2] as $key=$value) {
$excel-getActiveSheet()-setCellValue("$letter[$j]$i","$value");
$j++;
}
}
6.
//創(chuàng)建Excel輸入對(duì)象
$write = new PHPExcel_Writer_Excel5($excel);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");;
header('Content-Disposition:attachment;filename="testdata.xls"');
header("Content-Transfer-Encoding:binary");
$write-save('php://output');
7.打開頁(yè)面,刷新的時(shí)候會(huì)彈出對(duì)話框,讓你選擇文件保存路徑和文件名稱,
8.打開表格后,數(shù)據(jù)和格式跟代碼中的一致,說(shuō)明PHP導(dǎo)出的Excel是正確的。如果出現(xiàn)錯(cuò)誤,檢查一下你的表格數(shù)組和數(shù)據(jù)數(shù)組吧。
本文題目:php導(dǎo)出數(shù)據(jù)到pdf php如何導(dǎo)出數(shù)據(jù)庫(kù)
瀏覽路徑:http://www.chinadenli.net/article48/dooejhp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、網(wǎng)站導(dǎo)航、面包屑導(dǎo)航、品牌網(wǎng)站建設(shè)、全網(wǎng)營(yíng)銷推廣、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)