Re: Python script for generate cross-references in .pdf file
@Alfonso, thanks for your compliments.
You are not logged in. Please login or register.
QElectroTech → Scripts → Python script for generate cross-references in .pdf file
@Alfonso, thanks for your compliments.
**Native PDF hyperlinks for cross-references – Feature Request**
Currently, QElectroTech exports PDF files using Qt's `QPrinter`/`QPainter`, which renders content flat — text and shapes are drawn correctly, but there is no support for embedded hyperlink annotations in the PDF output.
This is why the workaround Python script (using PyMuPDF/fitz) was created: it post-processes the exported PDF to detect cross-reference labels, calculate target page positions, and inject clickable link annotations (`/Annots`) into the PDF.
It would be great to have this functionality built directly into QET's PDF export.
**Why Qt5 is a dead end**
Qt5 has reached its end of life. No security patches, no bug fixes unless they also affect Qt6. Linux distributions are already phasing it out. The migration to Qt6 is not optional — it is a matter of survival for the project. Any energy invested in patching the PDF export on Qt5 is wasted effort.
**What Qt6 brings — and what it still doesn't**
Despite ongoing improvements to `QPdfWriter` across Qt6 releases (ColorModel, CMYK, OutputIntent...), there is still no official public API for writing link annotations (`/Annots`) as of Qt 6.11. This limitation has actually been tracked since 2015 (QTBUG-44563) with no resolution to date.
However, Qt's internal PDF engine does have a private method — `QPdfEngine::drawHyperlink(QRect, QUrl)` — that can write link annotations. It is accessible (unofficially) by casting the paint engine:
```cpp
dynamic_cast<QPdfEngine*>(painter.paintEngine())->drawHyperlink(rect, url);
```
This requires including the private header `qpdf_p.h` and is not part of the public API, so it could break between Qt versions. But it proves the underlying engine already supports it — it just isn't exposed.
**The right approach**
There are three realistic paths for QET:
1. **Use `QPdfEngine::drawHyperlink` (private API)** — works today, no external dependency, but fragile across Qt version updates.
2. **Port the Python post-processing to C++** — using a library such as libmupdf or QPDF to inject `/Annots` after export, same logic as Alfonso's script but integrated natively.
3. **Wait for Qt to expose this officially** — unlikely in the short term given QTBUG-44563 has been open for 10 years.
The Qt6 migration is the right moment to tackle this. The PDF export code will be touched anyway, and option 1 could be a pragmatic first step while option 2 is planned as a more robust long-term solution.
Thanks to Alfonso and the community for the Python workaround — it proves the concept and shows this is a real user need.
QElectroTech → Scripts → Python script for generate cross-references in .pdf file
Powered by PunBB, supported by Informer Technologies, Inc.
Generated in 0.017 seconds (24% PHP - 76% DB) with 11 queries