gpt4 book ai didi

tcpdf - 使用FPDI保留内部链接

转载 作者:行者123 更新时间:2023-12-04 08:55:15 28 4
gpt4 key购买 nike

我正在尝试向现有的pdf文件中动态添加一些文本。

我已经尝试过将FPDF和TCPDF与FPDI结合使用,以导入现有的pdf。没关系。但是,正如预期的那样,原始pdf的所有现有链接都消失了。

然后,我尝试使用此FPDI扩展来保留链接:

fpdi_with_annnots
https://gist.github.com/andreyvit/2020422

最初,它仅保留外部链接,但是随后,创建者进行了修改,使其还包括内部链接。但是此扩展名很旧,不再维护,也不再适用于FPDI和TCPDF的**内部链接**(保留外部链接,没关系!)。

有人尝试(请参见上面的Github链接)使其与TCPDF一起使用,并更改了这段代码:

$this->PageLinks[$this->page][] = $link;

对此:
$this->Link(
$link[0]/$this->k,
($this->fhPt-$link[1]+$link[3])/$this->k,
$link[2]/$this->k,
-$link[3]/$this->k,
$link[4]
);

然后,一段时间后,有人说需要将其更改为:
$this->Link(
$link[0]/$this->k,
($this->hPt - $link[1])/$this->k,
$link[2]/$this->k,
$link[3]/$this->k,
$link[4]
);

但它也不再起作用。

问题:

1)有人知道如何更改此代码以保留内部链接吗?
或者:
2)有谁知道fpdi_with_annots的替代方案,它可以导入,生成和保留超链接?

小费:
也许为FPDF使用“书签”扩展名会有所帮助,而不是使用Addlink()和Setlink():
http://fpdf.de/downloads/addons/1/

最佳答案

TCPDF + FPDI方法也保持内部和外部链接
在处理PDF时,这将保留您的内部和外部链接。它尚未经过全面测试,但应该可以正常工作。

<?php

use setasign\Fpdi\PdfParser\PdfParser;
use setasign\Fpdi\PdfParser\Type\PdfArray;
use setasign\Fpdi\PdfParser\Type\PdfDictionary;
use setasign\Fpdi\PdfParser\Type\PdfIndirectObjectReference;
use setasign\Fpdi\PdfParser\Type\PdfType;
use setasign\Fpdi\PdfReader\PageBoundaries;
use setasign\Fpdi\Tcpdf\Fpdi;

class TcpdfFpdiCustom extends Fpdi
{
public $pagesList;

public function importPage($pageNumber, $box = PageBoundaries::CROP_BOX, $groupXObject = true)
{
$pageId = parent::importPage($pageNumber, $box, $groupXObject);

$links = [];
$reader = $this->getPdfReader($this->currentReaderId);
$parser = $reader->getParser();

if (empty($this->pagesList)) {
$this->readAllPages($parser);
}

$pageObj = $reader->getPage($pageNumber)->getPageObject();
$annotationsObject = PdfDictionary::get(PdfType::resolve($pageObj, $parser), 'Annots');
$annotations = PdfType::resolve($annotationsObject, $parser);

if ($annotations->value) {
foreach ($annotations->value as $annotationRef) {
$annotation = PdfType::resolve($annotationRef, $parser);

if (PdfDictionary::get($annotation, 'Subtype')->value !== 'Link' ||
!$a = PdfDictionary::get($annotation, 'A')
) {
continue;
}

$link = PdfType::resolve($a, $parser);
$linkType = PdfDictionary::get($link, 'S')->value;

if (in_array($linkType, ['URI', 'GoTo']) &&
($rect = PdfDictionary::get($annotation, 'Rect')) &&
$rect instanceof PdfArray
) {
$rect = $rect->value;

$links[] = [
$rect[0]->value,
$rect[1]->value,
$rect[2]->value - $rect[0]->value,
$rect[1]->value - $rect[3]->value,
$this->getAnnotationLink($link, $linkType)
];
}
}
}

$this->importedPages[$pageId]['links'] = $links;

return $pageId;
}

public function useTemplate($tpl, $x = 0, $y = 0, $width = null, $height = null, $adjustPageSize = false)
{
$size = parent::useTemplate($tpl, $x, $y, $width, $height, $adjustPageSize);

$links = $this->importedPages[$tpl]['links'];
$pxToU = $this->pixelsToUnits(1);
foreach ($links as $link) {
// When is integer, it means that is an internal link
if (is_int($link[4])) {
$l = $this->AddLink();
$this->SetLink($l, 0, $link[4]);
$link[4] = $l;
}

$this->Link(
$link[0] * $pxToU,
$this->getPageHeight() - $link[1] * $pxToU,
$link[2] * $pxToU,
$link[3] * $pxToU,
$link[4]
);
}

return $size;
}

public function readAllPages(PdfParser $parser)
{
$readPages = function ($kids, $count) use (&$readPages, $parser) {
$kids = PdfArray::ensure($kids);
$isLeaf = ($count->value === \count($kids->value));

foreach ($kids->value as $reference) {
$reference = PdfIndirectObjectReference::ensure($reference);

if ($isLeaf) {
$this->pagesList[] = $reference;
continue;
}

$object = $parser->getIndirectObject($reference->value);
$type = PdfDictionary::get($object->value, 'Type');

if ($type->value === 'Pages') {
$readPages(PdfDictionary::get($object->value, 'Kids'), PdfDictionary::get($object->value, 'Count'));
} else {
$this->pagesList[] = $object;
}
}
};

$catalog = $parser->getCatalog();
$pages = PdfType::resolve(PdfDictionary::get($catalog, 'Pages'), $parser);
$count = PdfType::resolve(PdfDictionary::get($pages, 'Count'), $parser);
$kids = PdfType::resolve(PdfDictionary::get($pages, 'Kids'), $parser);
$readPages($kids, $count);
}

public function getAnnotationLink(PdfType $link, string $linkType)
{
// External links
if ($linkType === 'URI') {
return PdfDictionary::get($link, 'URI')->value;
}

// Internal links
if (!empty($this->pagesList)) {
$pageObj = PdfDictionary::get($link, 'D')->value[0];
foreach ($this->pagesList as $index => $page) {
if ($page->generationNumber === $pageObj->generationNumber && $page->value === $pageObj->value) {
return $index + 1;
}
}
}

return null;
}
}
用法
用以下代码替换Fpdi构造函数:
$pdf = new TcpdfFpdiCustom();
使用的Composer软件包:
"require": {
"setasign/fpdi": "^2.3",
"tecnickcom/tcpdf": "^6.4",
}

关于tcpdf - 使用FPDI保留内部链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29968366/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com