Append all at once

This commit is contained in:
Kovid Goyal 2023-06-17 09:55:52 +05:30
parent a610fc4a27
commit 7469c920f2
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 9 additions and 8 deletions

View File

@ -1134,7 +1134,7 @@ def convert(opf_path, opts, metadata=None, output_path=None, log=default_log, co
results = manager.convert_html_files(jobs, settle_time=1, has_maths=has_maths) results = manager.convert_html_files(jobs, settle_time=1, has_maths=has_maths)
num_pages = 0 num_pages = 0
page_margins_map = [] page_margins_map = []
log(f'Merging {len(margin_files)} PDF render results, this could take a while...') all_docs = []
for i, margin_file in enumerate(margin_files): for i, margin_file in enumerate(margin_files):
name = margin_file.name name = margin_file.name
data = results[name] data = results[name]
@ -1145,13 +1145,10 @@ def convert(opf_path, opts, metadata=None, output_path=None, log=default_log, co
doc_pages = doc.page_count() doc_pages = doc.page_count()
page_margins_map.extend(repeat(resolve_margins(margin_file.margins, page_layout), doc_pages)) page_margins_map.extend(repeat(resolve_margins(margin_file.margins, page_layout), doc_pages))
num_pages += doc_pages num_pages += doc_pages
all_docs.append(doc)
if pdf_doc is None: pdf_doc = all_docs[0]
pdf_doc = doc pdf_doc.append(*all_docs[1:])
else:
st = monotonic()
pdf_doc.append(doc)
log(f'Merged ({i}/{len(margin_files)-1}) in {monotonic()-st:.1f} seconds')
page_number_display_map = get_page_number_display_map(manager, opts, num_pages, log) page_number_display_map = get_page_number_display_map(manager, opts, num_pages, log)

View File

@ -11,6 +11,7 @@
#include <new> #include <new>
#include <string_view> #include <string_view>
#include <unordered_map> #include <unordered_map>
#include <vector>
using namespace pdf; using namespace pdf;
@ -365,12 +366,15 @@ PDFDoc_append(PDFDoc *self, PyObject *args) {
PdfMemDocument *dest = self->doc; PdfMemDocument *dest = self->doc;
try { try {
std::vector<const PdfMemDocument*> docs(PyTuple_GET_SIZE(args));
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(args); i++) { for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(args); i++) {
PyObject *doc = PyTuple_GET_ITEM(args, i); PyObject *doc = PyTuple_GET_ITEM(args, i);
int typ = PyObject_IsInstance(doc, (PyObject*)&PDFDocType); int typ = PyObject_IsInstance(doc, (PyObject*)&PDFDocType);
if (typ == -1) return NULL; if (typ == -1) return NULL;
if (typ == 0) { PyErr_SetString(PyExc_TypeError, "You must pass a PDFDoc instance to this method"); return NULL; } if (typ == 0) { PyErr_SetString(PyExc_TypeError, "You must pass a PDFDoc instance to this method"); return NULL; }
const PdfMemDocument *src = ((PDFDoc*)doc)->doc; docs[i] = ((PDFDoc*)doc)->doc;
}
for (auto src : docs) {
std::unordered_map<PdfReference, PdfObject*> ref_map; std::unordered_map<PdfReference, PdfObject*> ref_map;
std::unordered_map<PdfReference, PdfReference> page_parent_map; std::unordered_map<PdfReference, PdfReference> page_parent_map;
const unsigned initial_page_count = dest->GetPages().GetCount(); const unsigned initial_page_count = dest->GetPages().GetCount();