This commit is contained in:
Kovid Goyal 2025-03-16 17:24:00 +05:30
parent d422c4b7f4
commit 3eb3484cc0
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 14 additions and 9 deletions

View File

@ -242,6 +242,8 @@ def test_podofo():
try: try:
p = podofo.PDFDoc() p = podofo.PDFDoc()
p.open(f.name) p.open(f.name)
print(1111111111111111111)
print(22222222, repr(p.title))
if (p.title, p.author, p.keywords) != ('info title', 'info author', 'a, b'): if (p.title, p.author, p.keywords) != ('info title', 'info author', 'a, b'):
raise ValueError('podofo failed to set title and author in Info dict {} != {}'.format( raise ValueError('podofo failed to set title and author in Info dict {} != {}'.format(
(p.title, p.author, p.keywords), ('info title', 'info author', 'a, b'))) (p.title, p.author, p.keywords), ('info title', 'info author', 'a, b')))

View File

@ -731,19 +731,21 @@ PDFDoc_version_getter(PDFDoc *self, void *closure) {
return PyUnicode_FromString(""); return PyUnicode_FromString("");
} }
static PdfDictionary& static void
get_or_create_info(PDFDoc *self) { get_or_create_info(PDFDoc *self, PdfDictionary **dict) {
PdfObject *info = self->doc->GetTrailer().GetDictionary().FindKey("Info"); PdfObject *info = self->doc->GetTrailer().GetDictionary().FindKey("Info");
if (info && info->IsDictionary()) return info->GetDictionary(); if (info && info->TryGetDictionary(*dict)) return;
info = &self->doc->GetObjects().CreateDictionaryObject(); info = &self->doc->GetObjects().CreateDictionaryObject();
self->doc->GetTrailer().GetDictionary().AddKeyIndirect("Info", *info); self->doc->GetTrailer().GetDictionary().AddKeyIndirect("Info", *info);
return info->GetDictionary(); info->TryGetDictionary(*dict);
} }
static inline PyObject* static inline PyObject*
string_metadata_getter(PDFDoc *self, const std::string_view name) { string_metadata_getter(PDFDoc *self, const std::string_view name) {
auto info = get_or_create_info(self); PdfDictionary *info_dict;
auto obj = info.FindKey(name); get_or_create_info(self, &info_dict);
auto obj = info_dict->FindKey(name);
const PdfString* str; const PdfString* str;
return (obj == nullptr || !obj->TryGetString(str)) ? PyUnicode_FromString("") : podofo_convert_pdfstring(*str); return (obj == nullptr || !obj->TryGetString(str)) ? PyUnicode_FromString("") : podofo_convert_pdfstring(*str);
} }
@ -782,11 +784,12 @@ PDFDoc_producer_getter(PDFDoc *self, void *closure) {
static inline int static inline int
string_metadata_setter(PDFDoc *self, const std::string_view name, PyObject *val) { string_metadata_setter(PDFDoc *self, const std::string_view name, PyObject *val) {
if (!PyUnicode_Check(val)) { PyErr_SetString(PyExc_TypeError, "Must use unicode to set metadata"); return -1; } if (!PyUnicode_Check(val)) { PyErr_SetString(PyExc_TypeError, "Must use unicode to set metadata"); return -1; }
auto& info = get_or_create_info(self); PdfDictionary *info_dict;
get_or_create_info(self, &info_dict);
const char *raw; Py_ssize_t sz; const char *raw; Py_ssize_t sz;
raw = PyUnicode_AsUTF8AndSize(val, &sz); raw = PyUnicode_AsUTF8AndSize(val, &sz);
if (sz == 0) info.RemoveKey(name); if (sz == 0) info_dict->RemoveKey(name);
else info.AddKey(name, PdfString(std::string_view(raw, sz))); else info_dict->AddKey(name, PdfString(std::string_view(raw, sz)));
return 0; return 0;
} }