Skip to content

Commit

Permalink
PDF metadata: Fix a regression that broke updating metadata in PDF fi…
Browse files Browse the repository at this point in the history
…les without an /Info dictionary
  • Loading branch information
kovidgoyal committed Sep 28, 2023
1 parent 1f29656 commit 82846b4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
10 changes: 9 additions & 1 deletion src/calibre/utils/podofo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,5 +259,13 @@ def test_podofo():
raise ValueError('Appending failed')


def develop(path=sys.argv[-1]):
podofo = get_podofo()
p = podofo.PDFDoc()
p.open(path)
p.title = 'test'



if __name__ == '__main__':
get_xmp_metadata(sys.argv[-1])
develop()
48 changes: 39 additions & 9 deletions src/calibre/utils/podofo/doc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,9 @@ static PdfDictionary&
get_or_create_info(PDFDoc *self) {
PdfObject *info = self->doc->GetTrailer().GetDictionary().FindKey("Info");
if (info && info->IsDictionary()) return info->GetDictionary();
auto ninfo = self->doc->GetObjects().CreateDictionaryObject();
self->doc->GetTrailer().GetDictionary().AddKeyIndirect("Info", ninfo);
return ninfo.GetDictionary();
info = &self->doc->GetObjects().CreateDictionaryObject();
self->doc->GetTrailer().GetDictionary().AddKeyIndirect("Info", *info);
return info->GetDictionary();
}

static inline PyObject*
Expand Down Expand Up @@ -797,32 +797,62 @@ string_metadata_setter(PDFDoc *self, const std::string_view name, PyObject *val)

static int
PDFDoc_title_setter(PDFDoc *self, PyObject *val, void *closure) {
return string_metadata_setter(self, "Title", val);
try {
return string_metadata_setter(self, "Title", val);
} catch(const PdfError & err) {
podofo_set_exception(err);
return -1;
}
}

static int
PDFDoc_author_setter(PDFDoc *self, PyObject *val, void *closure) {
return string_metadata_setter(self, "Author", val);
try {
return string_metadata_setter(self, "Author", val);
} catch(const PdfError & err) {
podofo_set_exception(err);
return -1;
}
}

static int
PDFDoc_subject_setter(PDFDoc *self, PyObject *val, void *closure) {
return string_metadata_setter(self, "Subject", val);
try {
return string_metadata_setter(self, "Subject", val);
} catch(const PdfError & err) {
podofo_set_exception(err);
return -1;
}
}

static int
PDFDoc_keywords_setter(PDFDoc *self, PyObject *val, void *closure) {
return string_metadata_setter(self, "Keywords", val);
try {
return string_metadata_setter(self, "Keywords", val);
} catch(const PdfError & err) {
podofo_set_exception(err);
return -1;
}
}

static int
PDFDoc_creator_setter(PDFDoc *self, PyObject *val, void *closure) {
return string_metadata_setter(self, "Creator", val);
try {
return string_metadata_setter(self, "Creator", val);
} catch(const PdfError & err) {
podofo_set_exception(err);
return -1;
}
}

static int
PDFDoc_producer_setter(PDFDoc *self, PyObject *val, void *closure) {
return string_metadata_setter(self, "Producer", val);
try {
return string_metadata_setter(self, "Producer", val);
} catch(const PdfError & err) {
podofo_set_exception(err);
return -1;
}
}

static PyGetSetDef PDFDoc_getsetters[] = {
Expand Down

0 comments on commit 82846b4

Please sign in to comment.