From 19ca8d8bd82ee6fb3a9b358050467a4087fe6b04 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 17 Jul 2019 09:37:13 +0530 Subject: [PATCH] Couple more PDF utilities --- src/calibre/utils/podofo/__init__.py | 20 +----------------- src/calibre/utils/podofo/doc.cpp | 31 ++++++++++++++++++++++------ 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/calibre/utils/podofo/__init__.py b/src/calibre/utils/podofo/__init__.py index a57d5c60ff..39c5eb2320 100644 --- a/src/calibre/utils/podofo/__init__.py +++ b/src/calibre/utils/podofo/__init__.py @@ -10,7 +10,7 @@ from calibre.constants import plugins, preferred_encoding from calibre.ebooks.metadata import authors_to_string from calibre.ptempfile import TemporaryDirectory from calibre.utils.ipc.simple_worker import WorkerError, fork_job -from polyglot.builtins import range, unicode_type, iteritems +from polyglot.builtins import unicode_type, iteritems def get_podofo(): @@ -106,24 +106,6 @@ def set_metadata_(tdir, title, authors, bkp, tags, xmp_packet): return touched -def delete_all_but(path, pages): - ''' Delete all the pages in the pdf except for the specified ones. Negative - numbers are counted from the end of the PDF. ''' - podofo = get_podofo() - p = podofo.PDFDoc() - with open(path, 'rb') as f: - raw = f.read() - p.load(raw) - total = p.page_count() - pages = {total + x if x < 0 else x for x in pages} - for page in range(total-1, -1, -1): - if page not in pages: - p.delete_page(page) - - with open(path, 'wb') as f: - p.save_to_fileobj(f) - - def get_xmp_metadata(path): podofo = get_podofo() p = podofo.PDFDoc() diff --git a/src/calibre/utils/podofo/doc.cpp b/src/calibre/utils/podofo/doc.cpp index 76e344289f..b6fea5e828 100644 --- a/src/calibre/utils/podofo/doc.cpp +++ b/src/calibre/utils/podofo/doc.cpp @@ -183,11 +183,11 @@ PDFDoc_image_count(PDFDoc *self, PyObject *args) { // delete_page() {{{ static PyObject * -PDFDoc_delete_page(PDFDoc *self, PyObject *args) { - int num = 0; - if (PyArg_ParseTuple(args, "i", &num)) { +PDFDoc_delete_pages(PDFDoc *self, PyObject *args) { + int page = 0, count = 1; + if (PyArg_ParseTuple(args, "i|i", &page, &count)) { try { - self->doc->DeletePages(num, 1); + self->doc->DeletePages(page - 1, count); } catch(const PdfError & err) { podofo_set_exception(err); return NULL; @@ -197,6 +197,22 @@ PDFDoc_delete_page(PDFDoc *self, PyObject *args) { Py_RETURN_NONE; } // }}} +// copy_page() {{{ +static PyObject * +PDFDoc_copy_page(PDFDoc *self, PyObject *args) { + int from = 0, to = 0; + if (!PyArg_ParseTuple(args, "ii", &from, &to)) return NULL; + try { + PdfPagesTree* tree = self->doc->GetPagesTree(); + PdfPage* page = tree->GetPage(from - 1); + tree->InsertPage(to - 1, page); + } catch(const PdfError & err) { + podofo_set_exception(err); + return NULL; + } + Py_RETURN_NONE; +} // }}} + // append() {{{ static PyObject * PDFDoc_append(PDFDoc *self, PyObject *args) { @@ -722,8 +738,11 @@ static PyMethodDef PDFDoc_methods[] = { {"remove_fonts", (PyCFunction)remove_fonts, METH_VARARGS, "remove_fonts() -> Remove the specified font objects." }, - {"delete_page", (PyCFunction)PDFDoc_delete_page, METH_VARARGS, - "delete_page(page_num) -> Delete the specified page from the pdf (0 is the first page)." + {"delete_pages", (PyCFunction)PDFDoc_delete_pages, METH_VARARGS, + "delete_page(page_num, count=1) -> Delete the specified pages from the pdf." + }, + {"copy_page", (PyCFunction)PDFDoc_copy_page, METH_VARARGS, + "copy_page(from, to) -> Copy the specified page." }, {"append", (PyCFunction)PDFDoc_append, METH_VARARGS, "append(doc) -> Append doc (which must be a PDFDoc) to this document."