1

EDIT: If you are using Bootstrap, then it is most likely the reason for this problem. Read here.

I'm trying to generate a PDF of a twig template using KnpSnappyBundle.

The problem is that the generated PDF is displaying the actual link in addition to the link text for all links when it is generated, like in this image:enter image description here

Is there an option for this within wkhtmltopdf? I've looked at the options using wkhtmltopdf -H and neither disable-external-links nor disable-internal-links resolves this.

Here is the code I'm using to generate the PDF:

$this->get('knp_snappy.pdf')->generateFromHtml(
        $this->renderView(
            $template,
            array(
                $key => $array
            )
        ),
        $this->container->getParameter("upload_dir") . '/' . $file,
        array(
            "print-media-type" => true,
            "disable-external-links" => true,
            "disable-internal-links" => true
        )
    );

And the HTML:

<a href="{{ path('work_descriptions') }}#{{ value.descriptionId }}" target="_blank" class="work-link"><strong><u>{{ title }}</u></strong></a>

And yes, I know the {{ title }} value does not include the actual link because when I use it outside of a link tag, the PDF displays it fine.

I'm trying my best to avoid hacky solutions, but I'm not sure what the problem is.

UPDATE: The problem happens regardless of whether twig variables are used or not.

Community
  • 1
  • 1
Henry A.
  • 391
  • 2
  • 19
  • 1
    what happens if you don't disable internal/external links? – Jason Roman Feb 01 '16 at 21:11
  • The only thing the disable external/internal links option does is remove the ability to click on links in the PDF, the text unfortunately remains the same either way. – Henry A. Feb 01 '16 at 21:24
  • 1
    I've had this exact issue before and I cannot remember what the issue was. Is the link you gave a valid anchor in the document? – Jason Roman Feb 01 '16 at 21:31
  • Yes, not getting any HTML validation errors regarding it either. Absolute URLs make no difference as well. – Henry A. Feb 01 '16 at 21:39

1 Answers1

1

What I ended up doing was adding another variable to send to the template to let it know when the template was going to be used for a PDF, then run the appropriate JavaScript to remove the links after checking if the variable is defined.

In PDF generation action:

$this->get('knp_snappy.pdf')->generateFromHtml(
            $this->renderView(
                "my-template.html.twig",
                array(
                    "templateData" => array(
                        "data" => $session->get("templateData"),
                        "pdf" => true //<--------- Check this variable in template
                    )
                ),
            ),
            $this->container->getParameter("upload_dir") . '/' . $file,
            array(
                "print-media-type" => true,
                "disable-external-links" => true,
                "disable-internal-links" => true
            )
);

In Twig:

{% if templateData.pdf is defined %}
   <script>
      $(".work-link").each(function()
      {
         $(this).prop("href", "#");
      });
   </script>
{% endif %}

This seems to be working okay for now.

Henry A.
  • 391
  • 2
  • 19