1 /** \file
2 * Native PDF import using libpoppler.
3 *
4 * Authors:
5 * miklos erdelyi
6 *
7 * Copyright (C) 2007 Authors
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 *
11 */
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
17 #ifdef HAVE_POPPLER
19 #include "goo/GooString.h"
20 #include "ErrorCodes.h"
21 #include "GlobalParams.h"
22 #include "PDFDoc.h"
23 #include "Page.h"
24 #include "Catalog.h"
26 #include "pdf-input.h"
27 #include "extension/system.h"
28 #include "extension/input.h"
29 #include "svg-builder.h"
30 #include "pdf-parser.h"
32 #include "document-private.h"
34 namespace Inkscape {
35 namespace Extension {
36 namespace Internal {
38 /**
39 * Parses the first page of the given PDF document using PdfParser.
40 */
41 SPDocument *
42 PdfInput::open(::Inkscape::Extension::Input * mod, const gchar * uri) {
44 // Initialize the globalParams variable for poppler
45 if (!globalParams) {
46 globalParams = new GlobalParams();
47 }
48 GooString *filename_goo = new GooString(uri);
49 PDFDoc *pdf_doc = new PDFDoc(filename_goo, NULL, NULL, NULL); // TODO: Could ask for password
50 if (!pdf_doc->isOk()) {
51 int error = pdf_doc->getErrorCode();
52 delete pdf_doc;
53 if (error == errEncrypted) {
54 g_message("Document is encrypted.");
55 } else {
56 g_message("Failed to load document from data (error %d)", error);
57 }
59 return NULL;
60 }
62 // Get needed page
63 int page_num = 1;
64 Catalog *catalog = pdf_doc->getCatalog();
65 Page *page = catalog->getPage(page_num);
67 SPDocument *doc = sp_document_new(NULL, TRUE, TRUE);
68 bool saved = sp_document_get_undo_sensitive(doc);
69 sp_document_set_undo_sensitive(doc, false); // No need to undo in this temporary document
71 // Create builder and parser
72 SvgBuilder *builder = new SvgBuilder(doc);
73 PdfParser *pdf_parser = new PdfParser(pdf_doc->getXRef(), builder, page_num-1, page->getRotate(),
74 page->getResourceDict(), page->getCropBox());
76 // Parse the document structure
77 Object obj;
78 page->getContents(&obj);
79 if (!obj.isNull()) {
80 pdf_parser->parse(&obj);
81 }
83 // Cleanup
84 obj.free();
85 delete pdf_parser;
86 delete builder;
87 delete pdf_doc;
89 // Restore undo
90 sp_document_set_undo_sensitive(doc, saved);
92 return doc;
93 }
95 #include "../clear-n_.h"
97 void
98 PdfInput::init(void) {
99 Inkscape::Extension::Extension * ext;
101 ext = Inkscape::Extension::build_from_mem(
102 "<inkscape-extension>\n"
103 "<name>PDF Input</name>\n"
104 "<id>org.inkscape.input.pdf</id>\n"
105 "<input>\n"
106 "<extension>.pdf</extension>\n"
107 "<mimetype>application/pdf</mimetype>\n"
108 "<filetypename>Adobe PDF (*.pdf) [native]</filetypename>\n"
109 "<filetypetooltip>PDF Document</filetypetooltip>\n"
110 "</input>\n"
111 "</inkscape-extension>", new PdfInput());
112 } // init
114 } } } /* namespace Inkscape, Extension, Implementation */
116 #endif /* HAVE_POPPLER */
118 /*
119 Local Variables:
120 mode:c++
121 c-file-style:"stroustrup"
122 c-file-offsets:((innamespace . 0)(inline-open . 0))
123 indent-tabs-mode:nil
124 fill-column:99
125 End:
126 */
127 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :