Skip to content

Commit

Permalink
Add debug PDF format with link annotations on elements with id
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Aug 3, 2024
1 parent d3c51ac commit 844dffa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
11 changes: 8 additions & 3 deletions weasyprint/anchors.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,18 @@ def gather_anchors(box, anchors, links, bookmarks, forms, parent_matrix=None,
links.append((link_type, target, rectangle, box))
if is_input:
forms[parent_form].append((box.element, box.style, rectangle))
if matrix and (has_bookmark or has_anchor):
pos_x, pos_y = matrix.transform_point(pos_x, pos_y)
if has_bookmark:
if matrix:
pos_x, pos_y = matrix.transform_point(pos_x, pos_y)
bookmark = (bookmark_level, bookmark_label, (pos_x, pos_y), state)
bookmarks.append(bookmark)
if has_anchor:
anchors[anchor_name] = (pos_x, pos_y, width, height)
pos_x1, pos_y1, pos_x2, pos_y2 = pos_x, pos_y, pos_x + width, pos_y + height
print('pos', pos_x1, pos_y1, pos_x2, pos_y2)
if matrix:
pos_x1, pos_y1 = matrix.transform_point(pos_x1, pos_y1)
pos_x2, pos_y2 = matrix.transform_point(pos_x2, pos_y2)
anchors[anchor_name] = (pos_x1, pos_y1, pos_x2, pos_y2)

for child in box.all_children():
gather_anchors(child, anchors, links, bookmarks, forms, matrix, parent_form)
Expand Down
4 changes: 2 additions & 2 deletions weasyprint/pdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..html import W3C_DATE_RE
from ..logger import LOGGER, PROGRESS_LOGGER
from ..matrix import Matrix
from . import pdfa, pdfua
from . import debug, pdfa, pdfua
from .fonts import build_fonts_dictionary
from .stream import Stream

Expand All @@ -17,7 +17,7 @@
write_pdf_attachment)

VARIANTS = {
name: data for variants in (pdfa.VARIANTS, pdfua.VARIANTS)
name: data for variants in (pdfa.VARIANTS, pdfua.VARIANTS, debug.VARIANTS)
for (name, data) in variants.items()}


Expand Down
38 changes: 38 additions & 0 deletions weasyprint/pdf/debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""PDF generation with debug information."""

import pydyf

from ..matrix import Matrix


def debug(pdf, metadata, document, page_streams, attachments, compress):
"""Set debug PDF metadata."""

# Add links on ids.
pages = zip(pdf.pages['Kids'][::3], document.pages, page_streams)
for pdf_page_number, document_page, stream in pages:
if not document_page.anchors:
continue

page = pdf.objects[pdf_page_number]
if 'Annots' not in page:
page['Annots'] = pydyf.Array()

for id, (x1, y1, x2, y2) in document_page.anchors.items():
# TODO: handle zoom correctly.
matrix = Matrix(0.75, 0, 0, 0.75) @ stream.ctm
x1, y1 = matrix.transform_point(x1, y1)
x2, y2 = matrix.transform_point(x2, y2)
annotation = pydyf.Dictionary({
'Type': '/Annot',
'Subtype': '/Link',
'Rect': pydyf.Array([x1, y1, x2, y2]),
'BS': pydyf.Dictionary({'W': 0}),
'P': page.reference,
'T': pydyf.String(id), # id added as metadata
})
pdf.add_object(annotation)
page['Annots'].append(annotation.reference)


VARIANTS = {'debug': (debug, {})}

0 comments on commit 844dffa

Please sign in to comment.