php pdf旋轉與合併
紀錄以前在處理網站pdf的方式,功能上是將上傳的圖片與pdf合併成一個檔案,需要注意的地方是pdf如果有加密會直接錯誤,需要使用者自行注意上傳的檔案。
使用的套件有兩個
tcpdf:https://tcpdf.org/
tcpdi:https://github.com/pauln/tcpdi_parser
合併
使用tcpdf套件加上別人寫的tcpdi插件
將圖片與pdf合併成一個檔案
require_once("plugin/tcpdf/tcpdf.php");
require_once("plugin/tcpdf/tcpdi.php");//別人寫的PDF合併插件 https://github.com/pauln/tcpdi_parser
//合併檔案 抓出需合併的檔名
$file_row = array();//放合併檔名
$c_arr = array();
//檔案路徑
$filebase = $_SERVER['DOCUMENT_ROOT']."/user_file/case_file/{$getparm["cid"]}/";
for($i=0;$i<count($file_row);$i++){
array_push($c_arr, $filebase.$file_row[$i]);
}
$pdf = new TCPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
foreach($c_arr as $k => $v){
if(strtolower(substr($v,-3))!='pdf'){
if(strtolower(substr($v,-3))=='jpg'
|| strtolower(substr($v,-3))=='png'
|| strtolower(substr($v,-3))=='gif'
|| strtolower(substr($v,-4))=='jpeg'){
//圖片檔
$temparr = explode(".",$v);
$tempext = $temparr[count($temparr)-1];
$pdf->AddPage('L');
$pdf->Image($v, 'C', 6, 280, 0, strtoupper($tempext), false, 'C', false, 300, 'C', false, false, 0, false, false, false);
}
}else{
$pagecount = $pdf->setSourceFile($v);
for ($i = 1; $i <= $pagecount; $i++) {
$tplidx = $pdf->importPage($i);
$size = $pdf->getTemplatesize($tplidx);
$orientation = ($size['w'] > $size['h']) ? 'L' : 'P';
//分大小 小區域 <209.90277777778 A4 < 297.03888888889 A3 每接+86?
if($size['w'] > $size['h']){
$tsvale = $size['h'];
}else{
$tsvale = $size['w'];
}
if($tsvale > 383){
$ptype = 'A2';
}else if($tsvale > 297){
$ptype = 'A3';
}else{
$ptype = 'A4';
}
$pdf->AddPage($orientation, $ptype);
$pdf->useTemplate($tplidx);
}
}
}
$pdf->Output();
exit;
旋轉
將pdf旋轉角度
require_once("plugin/tcpdf/tcpdf.php");
require_once("plugin/tcpdf/tcpdi.php");
$filebase = $_SERVER['DOCUMENT_ROOT']."/user_file/case_file/33312/test1qP.pdf";//旋轉的檔案
$pdf = new TCPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pagecount = $pdf->setSourceFile($filebase);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$rotatanum = 90;//旋轉角度
for ($i = 1; $i <= $pagecount; $i++) {
$tplidx = $pdf->importPage($i);
$size = $pdf->getTemplatesize($tplidx);
$orientation = ($size['w'] > $size['h']) ? 'L' : 'P';
//分大小 小區域 <209.90277777778 A4 < 297.03888888889 A3 每接+86?
if($size['w'] > $size['h']){
$tsvale = $size['h'];
}else{
$tsvale = $size['w'];
}
if($tsvale > 383){
$ptype = ['format' => 'A2', 'Rotate' => $rotatanum];
}else if($tsvale > 297){
$ptype = ['format' => 'A3', 'Rotate' => $rotatanum];
}else{
$ptype = ['format' => 'A4', 'Rotate' => $rotatanum];
}
$pdf->AddPage($orientation, $ptype);
$pdf->useTemplate($tplidx);
}
$pdf->Output();
//$pdf->Output($_SERVER['DOCUMENT_ROOT']."/user_file/case_file/33312/test1qP.pdf",'F');
exit;