Mercurial > hgrepos > Python2 > PyMuPDF
comparison src_classic/helper-convert.i @ 1:1d09e1dec1d9 upstream
ADD: PyMuPDF v1.26.4: the original sdist.
It does not yet contain MuPDF. This normally will be downloaded when
building PyMuPDF.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:37:51 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 1:1d09e1dec1d9 |
|---|---|
| 1 %{ | |
| 2 /* | |
| 3 # ------------------------------------------------------------------------ | |
| 4 # Copyright 2020-2022, Harald Lieder, mailto:harald.lieder@outlook.com | |
| 5 # License: GNU AFFERO GPL 3.0, https://www.gnu.org/licenses/agpl-3.0.html | |
| 6 # | |
| 7 # Part of "PyMuPDF", a Python binding for "MuPDF" (http://mupdf.com), a | |
| 8 # lightweight PDF, XPS, and E-book viewer, renderer and toolkit which is | |
| 9 # maintained and developed by Artifex Software, Inc. https://artifex.com. | |
| 10 # ------------------------------------------------------------------------ | |
| 11 */ | |
| 12 //----------------------------------------------------------------------------- | |
| 13 // Convert any MuPDF document to a PDF | |
| 14 // Returns bytes object containing the PDF, created via 'write' function. | |
| 15 //----------------------------------------------------------------------------- | |
| 16 PyObject *JM_convert_to_pdf(fz_context *ctx, fz_document *doc, int fp, int tp, int rotate) | |
| 17 { | |
| 18 pdf_document *pdfout = pdf_create_document(ctx); // new PDF document | |
| 19 int i, incr = 1, s = fp, e = tp; | |
| 20 if (fp > tp) { | |
| 21 incr = -1; // count backwards | |
| 22 s = tp; // adjust ... | |
| 23 e = fp; // ... range | |
| 24 } | |
| 25 fz_rect mediabox; | |
| 26 int rot = JM_norm_rotation(rotate); | |
| 27 fz_device *dev = NULL; | |
| 28 fz_buffer *contents = NULL; | |
| 29 pdf_obj *resources = NULL; | |
| 30 fz_page *page=NULL; | |
| 31 fz_var(dev); | |
| 32 fz_var(contents); | |
| 33 fz_var(resources); | |
| 34 fz_var(page); | |
| 35 for (i = fp; INRANGE(i, s, e); i += incr) { // interpret & write document pages as PDF pages | |
| 36 fz_try(ctx) { | |
| 37 page = fz_load_page(ctx, doc, i); | |
| 38 mediabox = fz_bound_page(ctx, page); | |
| 39 dev = pdf_page_write(ctx, pdfout, mediabox, &resources, &contents); | |
| 40 fz_run_page(ctx, page, dev, fz_identity, NULL); | |
| 41 fz_close_device(ctx, dev); | |
| 42 fz_drop_device(ctx, dev); | |
| 43 dev = NULL; | |
| 44 pdf_obj *page_obj = pdf_add_page(ctx, pdfout, mediabox, rot, resources, contents); | |
| 45 pdf_insert_page(ctx, pdfout, -1, page_obj); | |
| 46 pdf_drop_obj(ctx, page_obj); | |
| 47 } | |
| 48 fz_always(ctx) { | |
| 49 pdf_drop_obj(ctx, resources); | |
| 50 fz_drop_buffer(ctx, contents); | |
| 51 fz_drop_device(ctx, dev); | |
| 52 fz_drop_page(ctx, page); | |
| 53 page = NULL; | |
| 54 dev = NULL; | |
| 55 contents = NULL; | |
| 56 resources = NULL; | |
| 57 } | |
| 58 fz_catch(ctx) { | |
| 59 fz_rethrow(ctx); | |
| 60 } | |
| 61 } | |
| 62 // PDF created - now write it to Python bytearray | |
| 63 PyObject *r = NULL; | |
| 64 fz_output *out = NULL; | |
| 65 fz_buffer *res = NULL; | |
| 66 // prepare write options structure | |
| 67 pdf_write_options opts = { 0 }; | |
| 68 opts.do_garbage = 4; | |
| 69 opts.do_compress = 1; | |
| 70 opts.do_compress_images = 1; | |
| 71 opts.do_compress_fonts = 1; | |
| 72 opts.do_sanitize = 1; | |
| 73 opts.do_incremental = 0; | |
| 74 opts.do_ascii = 0; | |
| 75 opts.do_decompress = 0; | |
| 76 opts.do_linear = 0; | |
| 77 opts.do_clean = 1; | |
| 78 opts.do_pretty = 0; | |
| 79 | |
| 80 fz_try(ctx) { | |
| 81 res = fz_new_buffer(ctx, 8192); | |
| 82 out = fz_new_output_with_buffer(ctx, res); | |
| 83 pdf_write_document(ctx, pdfout, out, &opts); | |
| 84 unsigned char *c = NULL; | |
| 85 size_t len = fz_buffer_storage(ctx, res, &c); | |
| 86 r = PyBytes_FromStringAndSize((const char *) c, (Py_ssize_t) len); | |
| 87 } | |
| 88 fz_always(ctx) { | |
| 89 pdf_drop_document(ctx, pdfout); | |
| 90 fz_drop_output(ctx, out); | |
| 91 fz_drop_buffer(ctx, res); | |
| 92 } | |
| 93 fz_catch(ctx) { | |
| 94 fz_rethrow(ctx); | |
| 95 } | |
| 96 return r; | |
| 97 } | |
| 98 %} |
