Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to provide hyperlink within a PDF document created using QT



  • I am creating a PDF Document , I want to provide an Hyperlink such that when i press that Hyper link it should point to a particular table or some content in the same PDF file , I was able to do this in html file but i was not able to do it in PDF created using QTextDocument .
    Is there any way i can do this in PDF ?


  • Lifetime Qt Champion

    Hi
    As far as i know. its not possible directly
    but should work pretty fine via html

    QPrinter printer(QPrinter::HighResolution);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setOutputFileName("out.pdf");
    
    QTextDocument document;
    QString html = "<a href='http://www.forum.qt.io'>Qt</a>";
    document.setHtml(html);
    
    document.print( &printer ); 
    


  • @mrjj ya using this it worked in Html file but when i tried in pdf its not working , I mean the URL is coming but when i press it its not pointing to the table which i wanted instead it opens explorer and searches in the internet


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you post an example that generates such a PDF ?



  • @sgaist

    css +="<style type=\"text/css\">";
    css += "table.tbl1 {border-width: 2px;border-style: solid;border-color: black;margin-top: 0px;margin-bottom: 0px;color: black;}";
    css += "table.tbl1 td {border: 1px solid black;}";
    css += "table.tbl1 th {border: 1px solid black;}";
    css += "table.tbl1 td {padding: 3px;}";
    css += "table.tbl1 th {padding: 3px;font-size: 18px;}";
    css+="</style>";
    text +="<table width=\"100%\" cellspacing=\"0\" class=\"tbl1\">";
    text +=("<tr><th><center><span style =font-size:12pt;>Data</th>"\
            "<th><center><span style =font-size:12pt;>data</th>"\
            "<th><center><span style =font-size:12pt;> Result</th></tr>");
    text +=("<tr><td><center><span style =font-size:12pt;>Data</span></center></td>"\
            "<td><center><span style =font-size:12pt;>Data1</span></center></td>"\
            "<td><center><span style =font-size:12pt;>Result</span></center></td></tr>");
    text += "</table><br></html>";
    QPrinter printer(QPrinter::HighResolution);
            printer.setOutputFormat(QPrinter::PdfFormat);
            printer.setOutputFileName("out.pdf");
    
            QTextDocument document;
           
            QString link = "<a style=  \"text-decoration:none; \"font color=\"blue\"; href=\"#DATA 1 STATUS\">DATA 1 STATUS</a>";
    
            QString header = "<h3 align=\"center\"><a name=\"DATA 1 STATUS\">DATA 1 STATUS</a></h3>";
            qRep = link+header+ css+text;
            document.setHtml(qRep);
    
            document.print( &printer );


  • @maniron28
    Your href link has spaces in it. There are sometimes issues with spaces in hrefs from PDF. First try substituting underscores or something, does that make it work from PDF? Then you could try %20 for the spaces, depending on how the HTML encoding performs that might work. Let us know if it is indeed a space problem?

    Also, your line

    QString link = "<a style=  \"text-decoration:none; \"font color=\"blue\"; href=\"#DATA 1 STATUS\">DATA 1 STATUS</a>";
    

    has incorrect quoting all over it. You may get away with that/have different behaviour in HTML5/browser compared to PDF. Get your quoting correct and try again?



  • @jonb Actually it is working , but not in the way i indented . I want the hyperlink to behave as a link . That is when pressed it should point to the header of a table , Instead it opens in the website as a link . This the issue faced by me when i create a PDF . But the same code works fine in HTML



  • @maniron28
    Then, if I understand right, have you checked what the behaviour of an <a href="..."> is supposed to be in a PDF file? Maybe you are supposed to use something else (e.g. some kind of PDF "bookmark" instead) if you want an "internal" jump to a place in a PDF document, as opposed to a regular HTML link, which causes a browser to open.



  • @jonb May be i should look that way , any suggestions on whether any of you under gone this situation?



  • @maniron28 Is there any solution available , i am not getting any



  • @maniron28
    (No, I have no experience of this.) I don't know if it will help but: try looking through your resulting PDF document (you can use a text editor). Locate where it has such a link. What "construct" is generated in the PDF for the <a href="..."> in the HTML? Then we know what that is, whether it is indeed for opening an external link. Maybe then we'll know it's not what it's supposed to be for an "internal bookmark" in PDF, and take it from there?


Log in to reply