Skip to content

Commit 99cb76a

Browse files
committed
Some Fixes
* Correctly compute PDF page number from given fz_location * Add mehod `apply_css` to reflowable documents.
1 parent 64104e2 commit 99cb76a

1 file changed

Lines changed: 50 additions & 9 deletions

File tree

src/__init__.py

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5767,6 +5767,24 @@ def layout(self, rect=None, width=0, height=0, fontsize=11):
57675767
self._reset_page_refs()
57685768
self.init_doc()
57695769

5770+
def apply_css(self, css: str, append: bool = True):
5771+
"""Apply CSS to a reflowable document.
5772+
5773+
If 'append' evaluates to True, the CSS will be appended to
5774+
the default CSS of this document type.
5775+
Otherwise the default CSS will be ignored.
5776+
"""
5777+
if self.is_closed or self.is_encrypted:
5778+
raise ValueError("document closed or encrypted")
5779+
doc = self.this
5780+
if not mupdf.fz_is_document_reflowable(doc):
5781+
return
5782+
if not isinstance(css, str) or not css:
5783+
return
5784+
append = int(bool(append)) # ensure integer
5785+
mupdf.fz_style_document(doc, append, css)
5786+
5787+
57705788
def load_page(self, page_id):
57715789
"""Load a page.
57725790

@@ -19107,6 +19125,12 @@ def JM_convert_to_pdf(doc, fp, tp, rotate) -> bytes:
1910719125
Convert any MuPDF document to a PDF
1910819126
Returns bytes object containing the PDF, created via 'write' function.
1910919127
'''
19128+
19129+
def loc_to_pno(doc, loc):
19130+
"""Convert location to page number."""
19131+
pno = sum(mupdf.fz_count_chapter_pages(doc, i) for i in range(loc.chapter))
19132+
return pno + loc.page
19133+
1911019134
pdfout = mupdf.PdfDocument()
1911119135
incr = 1
1911219136
s = fp
@@ -19117,6 +19141,7 @@ def JM_convert_to_pdf(doc, fp, tp, rotate) -> bytes:
1911719141
e = fp # ... range
1911819142
rot = JM_norm_rotation(rotate)
1911919143
i = fp
19144+
pno = -1 # init current PDF page number
1912019145
internal_links = [] # collect PDF-wide internal links here
1912119146
while 1: # interpret & write document pages as PDF pages
1912219147
if not _INRANGE(i, s, e):
@@ -19129,10 +19154,11 @@ def JM_convert_to_pdf(doc, fp, tp, rotate) -> bytes:
1912919154
dev = None
1913019155
page_obj = mupdf.pdf_add_page(pdfout, mediabox, rot, resources, contents)
1913119156
mupdf.pdf_insert_page(pdfout, -1, page_obj)
19157+
pno += 1
1913219158

1913319159
# also copy links to the output PDF page
1913419160
# get the PDF page we've just created
19135-
pdf_page = mupdf.pdf_load_page(pdfout, i)
19161+
pdf_page = mupdf.pdf_load_page(pdfout, pno)
1913619162

1913719163
# loop through source page links
1913819164
link = mupdf.fz_load_links(page) # load first link
@@ -19146,29 +19172,44 @@ def JM_convert_to_pdf(doc, fp, tp, rotate) -> bytes:
1914619172
else: # internal links done when PDF is complete
1914719173
# find target of internal link
1914819174
ret, xp, yp = mupdf.fz_resolve_link(doc, uri)
19149-
ilink={"page": i, "ret": ret, "from": rect, "h": rect.y1-rect.y0, "w": rect.x1-rect.x0, "xp": xp, "yp": yp}
19175+
if (ret.chapter, ret.page) == (-1, -1):
19176+
# could not resolve link destination
19177+
continue
19178+
ilink = {"page": pno,
19179+
"ret": ret,
19180+
"from": rect,
19181+
"h": rect.y1 - rect.y0,
19182+
"w": rect.x1 - rect.x0,
19183+
"xp": xp,
19184+
"yp": yp,
19185+
}
1915019186
internal_links.append(ilink)
1915119187
link = link.next()
19152-
19188+
1915319189
i += incr
1915419190

19155-
# PDF created - now write it to Python bytearray
1915619191
# insert any internal links collected before:
19192+
pno = None # init PDF page number
1915719193
for ilink in internal_links:
19158-
pdf_page = mupdf.pdf_load_page(pdfout, ilink["page"])
19194+
this_pno = ilink["page"]
19195+
if pno != this_pno:
19196+
pdf_page = mupdf.pdf_load_page(pdfout, this_pno)
19197+
pno = this_pno
1915919198
ret = ilink["ret"]
1916019199
dest = mupdf.fz_link_dest()
19161-
dest.type = 7 # XYZ destination format
19162-
dest.loc.chapter = ret.chapter
19163-
dest.loc.page = ret.page
19200+
dest.type = mupdf.FZ_LINK_DEST_XYZ
19201+
dest.loc.chapter = 0 # PDFs have 1 chapter
19202+
dest.loc.page = loc_to_pno(doc, ret)
1916419203
dest.h = ilink["h"]
1916519204
dest.w = ilink["w"]
1916619205
dest.x = ilink["xp"]
1916719206
dest.y = ilink["yp"]
1916819207
dest.zoom = 0
19169-
rect=ilink["from"]
19208+
rect = ilink["from"]
1917019209
uri = mupdf.pdf_new_uri_from_explicit_dest(mupdf.FzLinkDest(dest))
1917119210
mupdf.pdf_create_link(pdf_page, rect, uri)
19211+
19212+
# PDF created - now write it out
1917219213
# prepare write options structure
1917319214
opts = mupdf.PdfWriteOptions()
1917419215
opts.do_garbage = 3

0 commit comments

Comments
 (0)