diff options
author | Jeff Muizelaar <jeff@infidigm.net> | 2006-12-29 04:12:40 +0000 |
---|---|---|
committer | Jeff Muizelaar <jeff@infidigm.net> | 2006-12-29 04:12:40 +0000 |
commit | 42770e5f07407b03dce31c73fd6956f9c8fc9a06 (patch) | |
tree | 8a2d731abcd0f942105e781852c9649de585fc8f | |
parent | 6fedd991b584e300b5710630fa7942d357fe7aaa (diff) |
2006-12-28 Brad Taylor <brad@getcoded.net>
* poppler/glib/poppler-document.h:
* poppler/glib/poppler-document.cc: Add poppler_document_new_from_data
to allow loading PDFs out of memory.
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | glib/poppler-document.cc | 113 | ||||
-rw-r--r-- | glib/poppler-document.h | 4 |
3 files changed, 93 insertions, 30 deletions
@@ -1,3 +1,9 @@ +2006-12-28 Brad Taylor <brad@getcoded.net> + + * poppler/glib/poppler-document.h: + * poppler/glib/poppler-document.cc: Add poppler_document_new_from_data + to allow loading PDFs out of memory. + 2006-12-28 Albert Astals Cid <aacid@kde.org> * qt4/src/poppler-embeddedfile.cc: diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc index a96d89e1..66492775 100644 --- a/glib/poppler-document.cc +++ b/glib/poppler-document.cc @@ -61,6 +61,48 @@ struct _PopplerDocumentClass G_DEFINE_TYPE (PopplerDocument, poppler_document, G_TYPE_OBJECT); +static PopplerDocument * +_poppler_document_new_from_pdfdoc (PDFDoc *newDoc, + GError **error) +{ + PopplerDocument *document; + int err; + + document = (PopplerDocument *) g_object_new (POPPLER_TYPE_DOCUMENT, NULL, NULL); + + if (!newDoc->isOk()) { + err = newDoc->getErrorCode(); + delete newDoc; + if (err == errEncrypted) { + g_set_error (error, POPPLER_ERROR, + POPPLER_ERROR_ENCRYPTED, + "Document is encrypted."); + } else { + g_set_error (error, G_FILE_ERROR, + G_FILE_ERROR_FAILED, + "Failed to load document from data (error %d)'\n", + err); + } + + return NULL; + } + + document->doc = newDoc; + +#if defined (HAVE_CAIRO) + document->output_dev = new CairoOutputDev (); +#elif defined (HAVE_SPLASH) + SplashColor white; + white[0] = 255; + white[1] = 255; + white[2] = 255; + document->output_dev = new SplashOutputDev(splashModeRGB8, 4, gFalse, white); +#endif + document->output_dev->startDoc(document->doc->getXRef ()); + + return document; +} + /** * poppler_document_new_from_file: * @uri: uri of the file to load @@ -78,15 +120,12 @@ poppler_document_new_from_file (const char *uri, const char *password, GError **error) { - PopplerDocument *document; PDFDoc *newDoc; GooString *filename_g; GooString *password_g; int err; char *filename; - document = (PopplerDocument *) g_object_new (POPPLER_TYPE_DOCUMENT, NULL, NULL); - if (!globalParams) { globalParams = new GlobalParams("/etc/xpdfrc"); } @@ -106,38 +145,52 @@ poppler_document_new_from_file (const char *uri, if (password_g) delete password_g; - if (!newDoc->isOk()) { - err = newDoc->getErrorCode(); - delete newDoc; - if (err == errEncrypted) { - g_set_error (error, POPPLER_ERROR, - POPPLER_ERROR_ENCRYPTED, - "Document is encrypted."); - } else { - g_set_error (error, G_FILE_ERROR, - G_FILE_ERROR_FAILED, - "Failed to load document (error %d) '%s'\n", - err, - uri); - } + return _poppler_document_new_from_pdfdoc (newDoc, error); +} - return NULL; +/** + * poppler_document_new_from_data: + * @data: the pdf data contained in a char array + * @length: the length of #data + * @password: password to unlock the file with, or %NULL + * @error: Return location for an error, or %NULL + * + * Creates a new #PopplerDocument. If %NULL is returned, then @error will be + * set. Possible errors include those in the #POPPLER_ERROR and #G_FILE_ERROR + * domains. + * + * Return value: A newly created #PopplerDocument, or %NULL + **/ +PopplerDocument * +poppler_document_new_from_data (char *data, + int length, + const char *password, + GError **error) +{ + Object obj; + PDFDoc *newDoc; + MemStream *str; + GooString *password_g; + int err; + char *filename; + + if (!globalParams) { + globalParams = new GlobalParams("/etc/xpdfrc"); } + + // create stream + obj.initNull(); + str = new MemStream(data, 0, length, &obj); - document->doc = newDoc; + password_g = NULL; + if (password != NULL) + password_g = new GooString (password); -#if defined (HAVE_CAIRO) - document->output_dev = new CairoOutputDev (); -#elif defined (HAVE_SPLASH) - SplashColor white; - white[0] = 255; - white[1] = 255; - white[2] = 255; - document->output_dev = new SplashOutputDev(splashModeRGB8, 4, gFalse, white); -#endif - document->output_dev->startDoc(document->doc->getXRef ()); + newDoc = new PDFDoc(str, password_g, password_g); + if (password_g) + delete password_g; - return document; + return _poppler_document_new_from_pdfdoc (newDoc, error); } /** diff --git a/glib/poppler-document.h b/glib/poppler-document.h index d0b1591c..10f34b35 100644 --- a/glib/poppler-document.h +++ b/glib/poppler-document.h @@ -92,6 +92,10 @@ GType poppler_document_get_type (void) G_GNUC_CONST; PopplerDocument *poppler_document_new_from_file (const char *uri, const char *password, GError **error); +PopplerDocument *poppler_document_new_from_data (char *data, + int length, + const char *password, + GError **error); gboolean poppler_document_save (PopplerDocument *document, const char *uri, GError **error); |