atgp / factur-x

PHP library to manage your Factur-X / ZUGFeRD 2.0 PDF invoices files
MIT License
87 stars 22 forks source link

Hyperlinks in PDF #12

Closed seubi42 closed 10 months ago

seubi42 commented 2 years ago

Hi, I'am using this library which is really nice by the way. I found an issue when the source pdf contains hyperlinks. factur-x library seems to broke the hyperlinks in the PDF document.

The blue color and the underline style are still on the links but hitbox is removed to, so it is not possible to click on it anyome.

For information i use the following code :

` // 1. generate the pdf with dompdf $html = "my pdf with a <a href=\"https://github.com/atgp/factur-x/issues/new\">here link";

$options = new \Dompdf\Options(); $options->set('enable_php', true); $options->set('enable_html5_parser', true); $options->set('isRemoteEnabled', true); $options->set('dpi', 192); $dompdf = new \Dompdf\Dompdf($options); $dompdf->setHttpContext(stream_context_create([ 'ssl' => [ 'verify_peer' => FALSE, 'verify_peer_name' => FALSE, 'allow_self_signed'=> TRUE ], ])); $dompdf->loadHtml($html); $dompdf->setPaper('A4', 'portrait'); $dompdf->render(); pdfoutput = $dompdf->output(); // links in this pdf works

//2. factur x

$facturex_xml = "...custom code here to generate the right xml as string, using \horstoeko\zugferd\ZugferdDocumentBuilder library..."; $facturx = new \Atgp\FacturX\Facturx(); $facturxPdf = $facturx->generateFacturxFromFiles($pdfoutput, $facturex_xml); // links are now broken `

Is it a but to fix, or maybe i need to add some options in my code ? Thanks a lot for your help.

lucas-gp commented 1 year ago

Did you find a solution for this problem ? To know if I need to check it If not, could you give me an PDF example please ?

wq9578 commented 1 year ago

A closing </a> tag seems to be missing. (Also, PDF/A doesn't allow transparent objects, which might be relevant here.)

wq9578 commented 1 year ago

I now encountered the same problem. Links are not clickable anymore in the combined PDF. The text is still printed in blue color (marked as link), but is not clickable.

The PDF/A standard itself permits links:

Can PDF/A files include links?

There's no problem with including links in a PDF/A file. It should be considered, however, that external links will likely become invalid over time.

https://www.pdfa.org/pdfa-faq/

wq9578 commented 1 year ago

Example PDFs:

Created with atgp / factur-x (link not clickable) ZUGFeRD.pdf

Source PDF (link clickable) ZUGFeRD_part.pdf

Created with Mustang (link clickable) ZUGFeRD.pdf

lucas-gp commented 1 year ago

Hi @wq9578, It's seem it's linked to FPDI library that doesn't preserve external links I found several link about it :

So maybe need to make a custom class (or enhance FpdfiFacturx class) to be able to preverse external links Don't hesitate to contribute and make a merge request about it :)

wq9578 commented 1 year ago

I have no experience with PDF editing in PHP for making a pull request. But a quick internet search might suggest the following: The FPDI library is used by FPDF, which is in turn used by mPDF. Since external links in PDF/A documents work well with mPDF (I tested it), you might either have a look at how mPDF uses (indirectly) the FPDI library, or use mPDF directly.

JanSlabon commented 1 year ago

In FPDI 2.4.0 you can import PDF pages with external links now: https://github.com/Setasign/FPDI/releases/tag/v2.4.0 For backwards compatibility you need to activate this. See here for more details: https://www.setasign.com/news-archive/fpdi-2.4.0-released/

FactorSpeed commented 11 months ago

The problem seems to be in Facturx class :

$tplIdx = $pdfWriter->importPage($i, '/MediaBox');

if It's replaced by this below, It works well !

$tplIdx = $pdfWriter->importPage($i, '/MediaBox', true, true);
public function importPage(
        $pageNumber,
        $box = PageBoundaries::CROP_BOX,
        $groupXObject = true,
        $importExternalLinks = false // <= here
)
benito103e commented 10 months ago

Fixed into #28 : you can upgrade to v2 of this library.