PDF metadata: When updating the XMP metadata in PDF files, compress the metadata written to the PDF. This is particularly useful when the PDF file has an existing XMP metadata block with very large amounts of data in it. Fixes #1341549 [Embed Metadata for PDF consistently adds 0.5MB to book size](https://bugs.launchpad.net/calibre/+bug/1341549)

This commit is contained in:
Kovid Goyal 2014-07-15 23:01:27 +05:30
parent 245050d593
commit e1a1eb41f2

View File

@ -280,18 +280,20 @@ PDFDoc_set_xmp_metadata(PDFDoc *self, PyObject *args) {
long len = 0; long len = 0;
PoDoFo::PdfObject *metadata = NULL, *catalog = NULL; PoDoFo::PdfObject *metadata = NULL, *catalog = NULL;
PoDoFo::PdfStream *str = NULL; PoDoFo::PdfStream *str = NULL;
TVecFilters compressed(1);
compressed[0] = ePdfFilter_FlateDecode;
if (!PyArg_ParseTuple(args, "s#", &raw, &len)) return NULL; if (!PyArg_ParseTuple(args, "s#", &raw, &len)) return NULL;
try { try {
if ((metadata = self->doc->GetMetadata()) != NULL) { if ((metadata = self->doc->GetMetadata()) != NULL) {
if ((str = metadata->GetStream()) == NULL) { PyErr_NoMemory(); goto error; } if ((str = metadata->GetStream()) == NULL) { PyErr_NoMemory(); goto error; }
str->Set(raw, len, PoDoFo::TVecFilters()); str->Set(raw, len, compressed);
} else { } else {
if ((catalog = self->doc->GetCatalog()) == NULL) { PyErr_SetString(PyExc_ValueError, "Cannot set XML metadata as this document has no catalog"); goto error; } if ((catalog = self->doc->GetCatalog()) == NULL) { PyErr_SetString(PyExc_ValueError, "Cannot set XML metadata as this document has no catalog"); goto error; }
if ((metadata = self->doc->GetObjects().CreateObject("Metadata")) == NULL) { PyErr_NoMemory(); goto error; } if ((metadata = self->doc->GetObjects().CreateObject("Metadata")) == NULL) { PyErr_NoMemory(); goto error; }
if ((str = metadata->GetStream()) == NULL) { PyErr_NoMemory(); goto error; } if ((str = metadata->GetStream()) == NULL) { PyErr_NoMemory(); goto error; }
metadata->GetDictionary().AddKey(PoDoFo::PdfName("Subtype"), PoDoFo::PdfName("XML")); metadata->GetDictionary().AddKey(PoDoFo::PdfName("Subtype"), PoDoFo::PdfName("XML"));
str->Set(raw, len, PoDoFo::TVecFilters()); str->Set(raw, len, compressed);
catalog->GetDictionary().AddKey(PoDoFo::PdfName("Metadata"), metadata->Reference()); catalog->GetDictionary().AddKey(PoDoFo::PdfName("Metadata"), metadata->Reference());
} }
} catch(const PdfError & err) { } catch(const PdfError & err) {