简体   繁体   中英

How to rid of Save As dialog and yellow box in tcpdf

When I generate and save a document with a link in it using TCPDF. It is always showing a save as dialog before closing in the pdf reader.

"Do you want to save the changes to file.pdf before closing?"

Also there is a yellow note box in the chrome browser when I hover over the link... as shown in the image below.

My code with problem is like this.

$pdf->writeHTML('<a href="/wp-admin/admin.php?page=system_view_data&id=1">link</a>');

However, if my links are empty in my html then there is no problem while closing the pdf, also then there is no yellow box anymore in links. The following code for example is working fine.

$pdf->writeHTML('<a href="">link</a>');

Here is a complete example to reproduce the problem.

require_once __DIR__.'../../external_classes/TCPDF/tcpdf.php';
    $pdf = new \TCPDF();
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('ABC');
    $pdf->SetTitle('ABC');
    $pdf->SetSubject('ABC');

    $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, "ABC", "ABC");
    $pdf->setPrintFooter(false);
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    $pdf->SetMargins(PDF_MARGIN_LEFT, 10, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    $pdf->SetFont('helvetica', '', 10);
    $pdf->AddPage();
    $pdf->writeHTML('<a href="/wp-admin/admin.php?page=system_view_data&id=1">link</a>');
    ob_clean();
    $pdf->Output('example.pdf', 'D');

The above example will generate the output like the following image. Which will have both the problems, I have tested both on tcpdf version 5.9.x and 6.2.x.

Okay, found some explanation to both the problems.

1. First Problem: The issue of Save as Dialog

For some reason the tcpdf library is appending my website html at the end of the pdf. I found this out by viewing my pdf in pure text using a text editor.

Using an exist function immediately after output() will fix it.

$pdf->Output('example.pdf', 'D');
exit();

2. Second Problem: The issue of yellow link box in chrome

This is currently an unresolved active issue in chrome. Check this link for example on mPdf forum.

You may resolve the second problem by removing annotations at all.

  • Create your own class MyTCPDF extends \\TCPDF.
  • Copy to created class "protected function _putannotsobjs()" from tcpdf.php. You will override it.
  • Change this string in method form
    `$annots .= ' /Contents ' . $this->_textstring($pl['txt'], $annot_obj_id);`
    enter code here to
    `$annots .= ' /Contents ()';`
  • Use your class for generation pdf file.
  • You may put any logic in this method an disable annotations by constant or something else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM