Skip to content

Commit fa4efca

Browse files
committed
Fixes issue #173
also massaged some demo scripts
1 parent ffde3b1 commit fa4efca

15 files changed

+58
-55
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# PyMuPDF 1.13.5 [![Build Status](https://travis-ci.org/rk700/PyMuPDF.svg?branch=master)](https://travis-ci.org/rk700/PyMuPDF)
1+
# PyMuPDF 1.13.6 [![Build Status](https://travis-ci.org/rk700/PyMuPDF.svg?branch=master)](https://travis-ci.org/rk700/PyMuPDF)
22

33
![logo](https://github.com/rk700/PyMuPDF/blob/master/demo/pymupdf.jpg)
44

@@ -10,7 +10,7 @@ Release date: May 23, 2018
1010

1111
# Introduction
1212

13-
This is **version 1.13.5 of PyMuPDF (formerly python-fitz)**, a Python binding with support for [MuPDF 1.13.0](http://mupdf.com/) - "a lightweight PDF and XPS viewer".
13+
This is **version 1.13.6 of PyMuPDF (formerly python-fitz)**, a Python binding with support for [MuPDF 1.13.0](http://mupdf.com/) - "a lightweight PDF and XPS viewer".
1414

1515
MuPDF can access files in PDF, XPS, OpenXPS, CBZ, EPUB and FB2 (e-books) formats, and it is known for its top performance and high rendering quality.
1616

demo/curly-polygon.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
img.drawSquiggle(points[i], points[i+1], breadth = breadth)
5050

5151
img.finish(color = (0,0,1), fill = (1,1,0), closePath = False)
52+
5253
# adjust visible page to dimensions of the drawing
5354
page.setCropBox(img.rect)
5455
img.commit()

demo/demo-lowlevel.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,10 @@ def olTraversal(root):
4444
print('Outline of the document')
4545
olTraversal(ol)
4646

47-
# we can also save a table of contents as a XML or TXT file
48-
ol.saveXML(sys.argv[1]+'.xml')
49-
ol.saveText(sys.argv[1]+'.txt')
50-
else:
51-
print('No outline available')
52-
5347
# get the page number, which should start from 0
5448
pn = int(sys.argv[2])-1
5549
if pn > doc.pageCount:
56-
print '%s has %d pages only' % (sys.argv[1], doc.pageCount)
57-
exit(1)
50+
raise SystemExit('%s has %d pages only' % (sys.argv[1], doc.pageCount))
5851

5952
# get the page
6053
page = doc.loadPage(pn)

demo/demo.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
Remarks
1717
--------
1818
This demo version uses the normal user interface of PyMuPDF. If you are
19-
interested in more low level information, have a look at "demo_lowlevel.py" in
19+
interested in more low level information, have a look at "demo-lowlevel.py" in
2020
the same directory.
2121
2222
"""
@@ -45,31 +45,31 @@
4545
if doc.metadata[key]:
4646
print(' %s: %s' % (key.title(), doc.metadata[key]))
4747
print("")
48+
4849
# here we print out the outline of the document(if any)
49-
# first, we define a function for traversal
5050
toc = doc.getToC()
5151
if len(toc) == 0:
52-
print('No outline available')
52+
print('No Table of Contents available')
5353
else:
5454
print("Table of Contents:")
5555
print("------------------")
5656
for t in toc:
5757
print(" " * (t[0] - 1), t[0], t[1], "page", t[2])
5858

5959
print("")
60+
6061
# get the page number, which should start from 0
6162
pn = int(sys.argv[2])-1
6263
if pn > doc.pageCount:
63-
print('%s has %d pages only' % (sys.argv[1], doc.pageCount))
64-
raise SystemExit
64+
raise SystemExit('%s has %d pages only' % (sys.argv[1], doc.pageCount))
6565

6666
# get the page
6767
page = doc[pn]
6868

6969
# we can also get all the links in the current page
7070
links = page.getLinks()
7171
if len(links) == 0:
72-
print("Page has no links")
72+
print("No links on page", (pn + 1))
7373
else:
7474
print("Links on page %i:" % (pn + 1))
7575
print("------------------")
@@ -95,6 +95,7 @@
9595
# now we are ready for search, with max hit count limited to 16
9696
# the return result is a list of hit box rectangles
9797
res = page.searchFor(sys.argv[6], hit_max = 16)
98+
print("search text '%s' found %i on the page" % (sys.argv[6], len(res)))
9899
for r in res:
99100
# we invert the pixmap at the hit irect to highlight the search result
100101
pm.invertIRect(r.round())

demo/draw-sines.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ def rot_points(pnts, pb, alfa):
171171
img.insertTextbox(r1, "sine", color = red, fontsize = 8, morph = morph)
172172
r2 = fitz.Rect(rect.x0 + 15, rect.y1 - 10, rect.br)
173173
img.insertTextbox(r2, "cosine", color = blue, fontsize = 8, morph = morph)
174+
174175
img.commit() # commit with overlay = True
175176

176177
doc.save("draw-sines.pdf")

demo/fitz-logo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
(2) SVG-based logos are not always shown correctly. Use a different
2222
PDF converter like svglib if that occurs.
2323
24-
(3) Logos / watermarks are transparent for non-image document types.
25-
If you need transparency for images, you must convert it to a PDF
24+
(3) Logos / watermarks are transparent for all document types except images.
25+
If you need transparency here, you must convert it to a PDF
2626
"manually" like so:
2727
2828
pix = fitz.Pixmap(logo_fn)

demo/list-fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
a = a.next
2626

2727
"""
28-
Above script may produce the following output:
28+
Example output of above script:
2929
3030
Showing the form fields of page 0
3131
--------------------------------------------------------------------------------

demo/splitter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
PyMuPDF Example Script:
44
------------------------
55
6-
Split a given PDF into separate files with one page each.
6+
Split a given PDF into separate files of one page each.
77
For "input.pdf" the generated files are named "input-%i.pdf".
88
99
PyMuPDF license

demo/svg-logo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
svglib, reportlab
1919
'''
2020
from __future__ import print_function
21+
import sys
2122
import fitz
2223
from svglib.svglib import svg2rlg
23-
import sys
2424
doc_fn = sys.argv[1] # name of PDF file
2525
svg_fn = sys.argv[2] # name of SVG image file
2626

fitz/fitz.i

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ struct fz_document_s
147147
self.isClosed = False
148148
self.isEncrypted = 0
149149
self.metadata = None
150+
self.stream = stream # prevent garbage collection of it
150151
self.openErrCode = 0
151152
self.openErrMsg = ''
152153
self.FontInfos = []
@@ -158,7 +159,6 @@ struct fz_document_s
158159
self.openErrCode = self._getGCTXerrcode()
159160
self.openErrMsg = self._getGCTXerrmsg()
160161
self.thisown = True
161-
self.isClosed = False
162162
if self.needsPass:
163163
self.isEncrypted = 1
164164
else: # we won't init until doc is decrypted
@@ -219,6 +219,7 @@ struct fz_document_s
219219
self._outline = None
220220
self._reset_page_refs()
221221
self.metadata = None
222+
self.stream = None
222223
self.isClosed = True
223224
self.openErrCode = 0
224225
self.openErrMsg = ''
@@ -312,7 +313,7 @@ struct fz_document_s
312313
limits = pdf_dict_get(gctx, efiles, PDF_NAME_Limits);
313314
limit1 = NULL;
314315
limit2 = NULL;
315-
if (limits) // have name limits?
316+
if (limits) // have name limits?
316317
{
317318
limit1 = pdf_to_utf8(gctx, pdf_array_get(gctx, limits, 0));
318319
limit2 = pdf_to_utf8(gctx, pdf_array_get(gctx, limits, 1));
@@ -374,7 +375,6 @@ struct fz_document_s
374375
%feature("autodoc","Retrieve embedded file information given its entry number or name.") embeddedFileInfo;
375376
PyObject *embeddedFileInfo(PyObject *id)
376377
{
377-
PyObject *infodict = PyDict_New();
378378
pdf_document *pdf = pdf_document_from_fz_document(gctx, $self);
379379
Py_ssize_t name_len = 0;
380380
int n = -1;
@@ -383,22 +383,23 @@ struct fz_document_s
383383
fz_try(gctx)
384384
{
385385
assert_PDF(pdf);
386-
n = FindEmbedded(gctx, id, pdf);
386+
n = JM_FindEmbedded(gctx, id, pdf);
387387
if (n < 0) THROWMSG("entry not found");
388388
}
389389
fz_catch(gctx) return NULL;
390390
391+
PyObject *infodict = PyDict_New();
391392
// name of file entry
392393
name = pdf_to_utf8(gctx, pdf_portfolio_entry_name(gctx, pdf, n));
393394
PyDict_SetItemString(infodict, "name",
394-
JM_UNICODE(name, strlen(name)));
395+
JM_UNICODE(name, strlen(name)));
395396
pdf_obj *o = pdf_portfolio_entry_obj(gctx, pdf, n);
396397
name = pdf_to_utf8(gctx, pdf_dict_get(gctx, o, PDF_NAME_F));
397398
PyDict_SetItemString(infodict, "file",
398-
JM_UNICODE(name, strlen(name)));
399+
JM_UNICODE(name, strlen(name)));
399400
name = pdf_to_utf8(gctx, pdf_dict_get(gctx, o, PDF_NAME_Desc));
400401
PyDict_SetItemString(infodict, "desc",
401-
JM_UNICODE(name, strlen(name)));
402+
JM_UNICODE(name, strlen(name)));
402403
pdf_obj *olen = pdf_dict_getl(gctx, o, PDF_NAME_EF, PDF_NAME_F,
403404
PDF_NAME_Length, NULL);
404405
int len = -1;
@@ -423,8 +424,8 @@ struct fz_document_s
423424
int flen = 0, dlen = 0;
424425
if (filename) flen = (int) strlen(filename);
425426
if (desc) dlen = (int) strlen(desc);
426-
if ((flen == 0) && (dlen == 0)) THROWMSG("nothing to change");
427-
int n = FindEmbedded(gctx, id, pdf);
427+
if ((flen < 1) && (dlen < 1)) THROWMSG("nothing to change");
428+
int n = JM_FindEmbedded(gctx, id, pdf);
428429
if (n < 0) THROWMSG("entry not found");
429430
pdf_obj *entry = pdf_portfolio_entry_obj(gctx, pdf, n);
430431
@@ -452,13 +453,13 @@ struct fz_document_s
452453
%feature("autodoc","Retrieve embedded file content given its entry number or name.") embeddedFileGet;
453454
PyObject *embeddedFileGet(PyObject *id)
454455
{
455-
PyObject *cont = PyBytes_FromString("");
456+
PyObject *cont = NULL;
456457
pdf_document *pdf = pdf_document_from_fz_document(gctx, $self);
457458
fz_buffer *buf = NULL;
458459
fz_try(gctx)
459460
{
460461
assert_PDF(pdf);
461-
int i = FindEmbedded(gctx, id, pdf);
462+
int i = JM_FindEmbedded(gctx, id, pdf);
462463
if (i < 0) THROWMSG("entry not found");
463464
unsigned char *data;
464465
buf = pdf_portfolio_entry(gctx, pdf, i);
@@ -492,12 +493,12 @@ struct fz_document_s
492493
assert_PDF(pdf);
493494
}
494495
fz_catch(gctx) return -1;
495-
if (file_len == 0) // no filename given
496+
if (file_len == 0) // no filename given
496497
{
497498
f = name; // take the name
498499
file_len = name_len;
499500
}
500-
if (desc_len == 0) // no description given
501+
if (desc_len == 0) // no description given
501502
{
502503
d = name; // take the name
503504
desc_len = name_len;
@@ -668,6 +669,7 @@ struct fz_document_s
668669
THROWMSG("decrypted file - save to new");
669670
pdf_finish_edit(gctx, pdf);
670671
pdf_save_document(gctx, pdf, filename, &opts);
672+
pdf->dirty = 0;
671673
}
672674
fz_catch(gctx) return NULL;
673675
return NONE;
@@ -715,6 +717,7 @@ struct fz_document_s
715717
res = fz_new_buffer(gctx, 1024);
716718
out = fz_new_output_with_buffer(gctx, res);
717719
pdf_write_document(gctx, pdf, out, &opts);
720+
pdf->dirty = 0;
718721
len = fz_buffer_storage(gctx, res, &c);
719722
r = PyBytes_FromStringAndSize(c, len);
720723
}
@@ -1612,6 +1615,7 @@ if links:
16121615
self.thisown = False
16131616
self.__swig_destroy__(self)
16141617
self.Graftmaps = {}
1618+
self.stream = None
16151619
self._reset_page_refs = DUMMY
16161620
self.__swig_destroy__ = DUMMY
16171621
self.isClosed = True

0 commit comments

Comments
 (0)