php操作pdf文档输出,一个对pdf文档进行操作的类(PHP版)

上一篇发了一个对pdf文档进行"建立及操作PDF文件"的类,这里将介绍另一个pdf的类,主要是对pdf文档进行操作的.具体功能这里不是详细简介,其实我也刚刚接触的,反正功能不算弱,就推荐给大家了,如果用手头没有好的可以试一下这个的,呵呵

Imports a single page of an existing PDF document and adds text in front of it.

require_once(‘fpdi.php’);

// initiate FPDI

$pdf =& new FPDI();

// add a page

$pdf->AddPage();

$pdf->SetFont(‘Arial’,”,10);

// set the sourcefile

$pdf->setSourceFile(‘PDFDocument.pdf’);

// import MediaBox from page 1

$box = ‘/MediaBox’;

$pdf->Cell(60, 4, $box);

$tplIdx = $pdf->importPage(1, $box);

$size = $pdf->useTemplate($tplIdx, 10, 15, 50);

$pdf->Rect(10, 15, $size[‘w’], $size[‘h’]);

// reimport page ones CropBox

$box = ‘/CropBox’;

$pdf->Cell(60, 4, $box);

$tplIdx = $pdf->importPage(1, $box);

$size = $pdf->useTemplate($tplIdx, 70, 15, 50);

$pdf->Rect(70, 15, $size[‘w’], $size[‘h’]);

// reimport page ones TrimBox

$box = ‘/TrimBox’;

$pdf->Cell(60, 4, $box);

$tplIdx = $pdf->importPage(1, $box);

$size = $pdf->useTemplate($tplIdx, 130, 15, 50);

$pdf->Rect(130, 15, $size[‘w’], $size[‘h’]);

$pdf->ln(45);

// reimport page ones BleedBox

$box = ‘/BleedBox’;

$pdf->Cell(60, 4, $box);

$tplIdx = $pdf->importPage(1, $box);

$size = $pdf->useTemplate($tplIdx, 10, 60, 50);

$pdf->Rect(10, 60, $size[‘w’], $size[‘h’]);

// reimport page ones ArtBox

$box = ‘/ArtBox’;

$pdf->Cell(60, 4, $box);

$tplIdx = $pdf->importPage(1, $box);

$size = $pdf->useTemplate($tplIdx, 70, 60, 50);

$pdf->Rect(70, 60, $size[‘w’], $size[‘h’]);

$pdf->Output(‘newpdf.pdf’, ‘D’);

This demo creates a thumbnail overview of an existing PDF document.

require(‘fpdi.php’);

$pdf =& new FPDI();

$pagecount = $pdf->setSourceFile(‘pdfdoc.pdf’);

$w = $pdf->w/2 – 15;

$h = 0;

$_x = $x = 10;

$_y = $y = 10;

$pdf->addPage();

for ($n = 1; $n <= $pagecount; $n++) {

$tplidx = $pdf->ImportPage($n);

$size = $pdf->useTemplate($tplidx, $x, $y, $w);

$pdf->Rect($x, $y, $size[‘w’], $size[‘h’]);

$h = max($h, $size[‘h’]);

if ($n % 2 == 0) {

$y += $h+10;

$x = $_x;

$h = 0;

} else {

$x += $w+10;

}

if ($n % 4 == 0 && $n != $pagecount) {

$pdf->AddPage();

$x = $_x;

$y = $_y;

}

}

$pdf->Output(‘newpdf.pdf’, ‘D’);

A concatenation of PDF documents.

require(‘fpdi.php’);

class concat_pdf extends FPDI {

var $files = array();

function setFiles($files) {

$this->files = $files;

}

function concat() {

foreach($this->files AS $file) {

$pagecount = $this->setSourceFile($file);

for ($i = 1; $i <= $pagecount; $i++) {

$tplidx = $this->ImportPage($i);

$s = $this->getTemplatesize($tplidx);

$this->AddPage($s[‘h’] > $s[‘w’] ? ‘P’ : ‘L’);

$this->useTemplate($tplidx);

}

}

}

}

$pdf =& new concat_pdf();

$pdf->setFiles(array(‘pdfdoc1.pdf’, ‘pdfdoc2.pdf’, ‘pdfdoc3.pdf’));

$pdf->concat();

$pdf->Output(‘newpdf.pdf’, ‘D’);

Import different boxes of a single page.

require_once(‘fpdi.php’);

// initiate FPDI

$pdf =& new FPDI();

// add a page

$pdf->AddPage();

$pdf->SetFont(‘Arial’,”,10);

// set the sourcefile

$pdf->setSourceFile(‘PDFDocument.pdf’);

// import MediaBox from page 1

$box = ‘/MediaBox’;

$pdf->Cell(60, 4, $box);

$tplIdx = $pdf->importPage(1, $box);

$size = $pdf->useTemplate($tplIdx, 10, 15, 50);

$pdf->Rect(10, 15, $size[‘w’], $size[‘h’]);

// reimport page ones CropBox

$box = ‘/CropBox’;

$pdf->Cell(60, 4, $box);

$tplIdx = $pdf->importPage(1, $box);

$size = $pdf->useTemplate($tplIdx, 70, 15, 50);

$pdf->Rect(70, 15, $size[‘w’], $size[‘h’]);

// reimport page ones TrimBox

$box = ‘/TrimBox’;

$pdf->Cell(60, 4, $box);

$tplIdx = $pdf->importPage(1, $box);

$size = $pdf->useTemplate($tplIdx, 130, 15, 50);

$pdf->Rect(130, 15, $size[‘w’], $size[‘h’]);

$pdf->ln(45);

// reimport page ones BleedBox

$box = ‘/BleedBox’;

$pdf->Cell(60, 4, $box);

$tplIdx = $pdf->importPage(1, $box);

$size = $pdf->useTemplate($tplIdx, 10, 60, 50);

$pdf->Rect(10, 60, $size[‘w’], $size[‘h’]);

// reimport page ones ArtBox

$box = ‘/ArtBox’;

$pdf->Cell(60, 4, $box);

$tplIdx = $pdf->importPage(1, $box);

$size = $pdf->useTemplate($tplIdx, 70, 60, 50);

$pdf->Rect(70, 60, $size[‘w’], $size[‘h’]);

$pdf->Output(‘newpdf.pdf’, ‘D’);

此类需要两个文件:

文件1:点击本地下载

文件2:点击本地下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值