Skip to content

Commit 5b3d6a0

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 5b3d6a0

1 file changed

Lines changed: 48 additions & 10 deletions

File tree

src/__init__.py

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5767,6 +5767,23 @@ 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+
57705787
def load_page(self, page_id):
57715788
"""Load a page.
57725789

@@ -19107,6 +19124,12 @@ def JM_convert_to_pdf(doc, fp, tp, rotate) -> bytes:
1910719124
Convert any MuPDF document to a PDF
1910819125
Returns bytes object containing the PDF, created via 'write' function.
1910919126
'''
19127+
19128+
def loc_to_pno(doc, loc):
19129+
"""Convert location to page number."""
19130+
pno = sum(mupdf.fz_count_chapter_pages(doc, i) for i in range(loc.chapter))
19131+
return pno + loc.page
19132+
1911019133
pdfout = mupdf.PdfDocument()
1911119134
incr = 1
1911219135
s = fp
@@ -19117,6 +19140,7 @@ def JM_convert_to_pdf(doc, fp, tp, rotate) -> bytes:
1911719140
e = fp # ... range
1911819141
rot = JM_norm_rotation(rotate)
1911919142
i = fp
19143+
pno = -1 # init current PDF page number
1912019144
internal_links = [] # collect PDF-wide internal links here
1912119145
while 1: # interpret & write document pages as PDF pages
1912219146
if not _INRANGE(i, s, e):
@@ -19129,10 +19153,11 @@ def JM_convert_to_pdf(doc, fp, tp, rotate) -> bytes:
1912919153
dev = None
1913019154
page_obj = mupdf.pdf_add_page(pdfout, mediabox, rot, resources, contents)
1913119155
mupdf.pdf_insert_page(pdfout, -1, page_obj)
19156+
pno += 1
1913219157

1913319158
# also copy links to the output PDF page
1913419159
# get the PDF page we've just created
19135-
pdf_page = mupdf.pdf_load_page(pdfout, i)
19160+
pdf_page = mupdf.pdf_load_page(pdfout, pno)
1913619161

1913719162
# loop through source page links
1913819163
link = mupdf.fz_load_links(page) # load first link
@@ -19146,29 +19171,42 @@ def JM_convert_to_pdf(doc, fp, tp, rotate) -> bytes:
1914619171
else: # internal links done when PDF is complete
1914719172
# find target of internal link
1914819173
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}
19150-
internal_links.append(ilink)
19174+
if ret.chapter > -1 and ret.page > -1:
19175+
ilink = {"page": pno,
19176+
"ret": ret,
19177+
"from": rect,
19178+
"h": rect.y1 - rect.y0,
19179+
"w": rect.x1 - rect.x0,
19180+
"xp": xp,
19181+
"yp": yp,
19182+
}
19183+
internal_links.append(ilink)
1915119184
link = link.next()
19152-
19185+
1915319186
i += incr
1915419187

19155-
# PDF created - now write it to Python bytearray
1915619188
# insert any internal links collected before:
19189+
pno = None # init PDF page number
1915719190
for ilink in internal_links:
19158-
pdf_page = mupdf.pdf_load_page(pdfout, ilink["page"])
19191+
this_pno = ilink["page"]
19192+
if pno != this_pno:
19193+
pdf_page = mupdf.pdf_load_page(pdfout, this_pno)
19194+
pno = this_pno
1915919195
ret = ilink["ret"]
1916019196
dest = mupdf.fz_link_dest()
19161-
dest.type = 7 # XYZ destination format
19162-
dest.loc.chapter = ret.chapter
19163-
dest.loc.page = ret.page
19197+
dest.type = mupdf.FZ_LINK_DEST_XYZ
19198+
dest.loc.chapter = 0 # PDFs have 1 chapter
19199+
dest.loc.page = loc_to_pno(doc, ret)
1916419200
dest.h = ilink["h"]
1916519201
dest.w = ilink["w"]
1916619202
dest.x = ilink["xp"]
1916719203
dest.y = ilink["yp"]
1916819204
dest.zoom = 0
19169-
rect=ilink["from"]
19205+
rect = ilink["from"]
1917019206
uri = mupdf.pdf_new_uri_from_explicit_dest(mupdf.FzLinkDest(dest))
1917119207
mupdf.pdf_create_link(pdf_page, rect, uri)
19208+
19209+
# PDF created - now write it out
1917219210
# prepare write options structure
1917319211
opts = mupdf.PdfWriteOptions()
1917419212
opts.do_garbage = 3

0 commit comments

Comments
 (0)