PDA

View Full Version : How to work with internal anchors



MDCremer
8th October 2015, 22:08
Hello,

in my project documentation is important. I use QPrinter and QTextDocument to save the data into a pdf. Everything works fine so far (ok, not everything, I struggle with putting a watermark on every side, but that's not half as important as the question, I start this Thread about), except that I don't become internal links working.

I write HTML into a QByteArray and import that via setHtml into the QTextDocument (the style-stuff goes in via a separate QByteArray and setDefaultStyleSheet). Anchors are set with <a name='target'>, internal links are set with <a href='#target'>. Nothing special so far.

But in the pdf there are no internal links. The tooltip shows, that the path to the document is always preceding the '#target'. All links are external. Maybe that would be ok, but after the drive-letter the ':' seems to be corrupted. There is a narrow black block instead. The link simply doesn't work.

Is there a 'open Sesame' that I can use to get internal links working?

Thanks

MDCremer

MDCremer
11th October 2015, 00:00
Hello again,

ok, there are a few possibilities:
- my English is too bad and nobody understands me (I know, it's bad),
- the problem is harder than I thought,
- I missed to give a proper example.

At least the last point can be fixed:



QByteArray text;
text.append("<a name='title'>title</a><br><br><br><a href='#title'>Go to title!</a>\n");
QTextDocument output;
output.setHtml(text);
output.print(&qprinter); // generates the PDF


As you can see on the screenshot, the link is transformed. Instead of targeting to the internal anchor, there should be loaded a file 'title' in the local directory.

I use Qt5.5 on Windows 10.

Is there a way that I can use links targeting within a pdf?

11417

anda_skoa
11th October 2015, 10:17
Maybe that is currently not supported by Qt's PDF generator.

Have you tried both options?
1) QPrinter with an output file
2) QPdfWriter

Cheers,
_

MDCremer
11th October 2015, 11:58
Hello anda-skoa,

as far as I have understood, the concept of QPdfWriter is to be a paint-device. The documentation I've in mind is a several hundred pages strong document, the printout of a whole database, some sort of a lexicon. That's what I need internal links for, to jump from one part of the lexicon to another. Maybe because I worked several decades with LaTeX, painting one page after another isn't what I want (I dont work with LaTeX in this project, because Qt has a subset of HTML for richtext. That's fine. I only have to become the whole stuff into a pdf).

If there is no way using Qt to accomplish such a task, are there other c++-libraries out there? I switched with my project from .Net to Qt. On .Net I worked with iTextSharp. Is there a similar product (free) for c++ on the market?

Thanks

anda_skoa
11th October 2015, 12:08
as far as I have understood, the concept of QPdfWriter is to be a paint-device.

Yes, like QPrinter.
It is likely using the same PDF generator, but you won't know for sure until you try.
Hence me asking whether you have already tried.




If there is no way using Qt to accomplish such a task, are there other c++-libraries out there?

Very likely.

Cheers,
_

MDCremer
11th October 2015, 14:12
Hello once more,

I tested it with QPdfWriter. There is a difference indeed!

11422

You can see, it suggests to reload the document. But if I say to do so, this is the next message:

11423

Unfortunatly, the '#' is escaped. Consequently the next message is:

11424

The Link is gone!

But I assume that's the fault of the Acrobat Reader and is insofar not covered by this forum.

Regards

P.S.: I downloaded a Hex-Viewer and took a look at the generated PDF. Qt produces:

11425

A working example generated by iTextSharp has the format:

11426

As you can see, both links are saved without any reference to the file itself. This is obviously generated by the Acrobat Reader. The difference between the non working Qt example and the other, working example are the spaces (and in the working example there is no 0x0A between the /URI-tags). The spaces in the working example are missing.

That might be very interesting. But as long as I don't know how to tell Qt to don't use spaces the link will not work, I fear.

anda_skoa
12th October 2015, 09:20
That minor difference could mean that there is a bug in the PDF generator.
Have you checked the Qt bug database?

In any case it is good to know that QPdfWriter does indeed do things differently to usng QPrinter in PDF mode.

Cheers,
_

MDCremer
12th October 2015, 15:41
Hello anda_skoa,

I went to the Newbie-Forum with my question, because I'm really very new to Qt. Maybe that I have a little bit experience in programming throughout the years, but I'm really not familiar how to check the Qt bug database (in fact, the first time I hear about that). Where can I find it, how can I add info about the maybe wrong pdf-behavior?

(I know, spaces are always good for readability of code. But with that particular code only the reader has to do. And obviously the additional spaces don't lead to a higher readability. Besides of that, that's 3 Bytes of wasted memory for every link. I know, memory is cheap nowadays...)

I'm not too shure about the different behavior of QPdfWriter und QPrinter. As I looked after the PDF's with the Hex-Viewer there where no difference between them. Maybe I forgot the '#' in my first example with QPrinter. That's why the Acrobat Reader wants to open the file. Sorry.

Regards

anda_skoa
12th October 2015, 17:50
Qt's public bug tracker is at https://bugreports.qt.io

I did a quick search for "PDF links" and this one was the first hit: https://bugreports.qt.io/browse/QTBUG-12035

Not totally sure this is the same thing though.

Cheers,
_