Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to provide hyperlink within a PDF document created using QT
Forum Update on Monday, May 27th 2025

How to provide hyperlink within a PDF document created using QT

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    ManiRon28
    wrote on last edited by ManiRon28
    #1

    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 ?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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 ); 
      
      M 1 Reply Last reply
      1
      • mrjjM mrjj

        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 ); 
        
        M Offline
        M Offline
        ManiRon28
        wrote on last edited by
        #3

        @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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi and welcome to devnet,

          Can you post an example that generates such a PDF ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi and welcome to devnet,

            Can you post an example that generates such a PDF ?

            M Offline
            M Offline
            ManiRon28
            wrote on last edited by
            #5

            @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 );
            
            JonBJ 1 Reply Last reply
            0
            • M ManiRon28

              @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 );
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @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?

              M 1 Reply Last reply
              1
              • JonBJ JonB

                @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?

                M Offline
                M Offline
                ManiRon28
                wrote on last edited by
                #7

                @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

                JonBJ 1 Reply Last reply
                0
                • M ManiRon28

                  @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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @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.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    ManiRon28
                    wrote on last edited by
                    #9

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

                    M 1 Reply Last reply
                    0
                    • M ManiRon28

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

                      M Offline
                      M Offline
                      ManiRon28
                      wrote on last edited by
                      #10

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

                      JonBJ 1 Reply Last reply
                      0
                      • M ManiRon28

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

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @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?

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved