Question

I was wondering if anyone could instruct as how to replace hyperlinks with their actual html links using xhtml2pdf. So if I had a hyperlink in the PDF I created that read:

Google

It would replace it with:

<http://www.google.com>

This is the current simple function I am using:

import os
import sys
import cgi
import cStringIO
import logging

import xhtml2pdf.pisa as pisa

pisa.showLogging()

def testSimple(
    data = open('FILENAME').read(),
    dest="test.pdf":

    pdf = pisa.CreatePDF(
        cStringIO.StringIO(data),
        file(dest, "wb")
        )

    if pdf.err:
        dumpErrors(pdf)
    else:
        pisa.startViewer(dest)

testSimple()
Was it helpful?

Solution

This is a common requirement for print styles of web sites, where people cannot click the link but need to type it.

It can actually be achieved with CSS:

a:link:after, a:visited:after { content:" [" attr(href) "] "; }

It will depend on the CSS implementation of xhtmltopdf if this will work in your case, though.

You can use CSS selectors if you only want to apply this to certain links, for example

a.printable:link:after,
a.printable:visited:after {
    content:" [" attr(href) "] ";
}

will only affect links that have that class:

<a href="http://stackoverflow.com/" class="printable">Stack Overflow</a>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top