使用FPDI保留内部链接

时间:2015-04-30 12:56:11

标签: tcpdf fpdf fpdi

我试图动态地将一些文字添加到现有的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/

2 个答案:

答案 0 :(得分:1)

TCPD + 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",
}

答案 1 :(得分:0)

过去4年我一直在使用TCPDF,FPDF,FPDI,Imagick,Ghostscript,我确实理解你所面临的挑战,但不幸的是,技术还没有。所以答案是否定的。