diff --git a/filters/karbon/pdf/CMakeLists.txt b/filters/karbon/pdf/CMakeLists.txt --- a/filters/karbon/pdf/CMakeLists.txt +++ b/filters/karbon/pdf/CMakeLists.txt @@ -14,3 +14,13 @@ target_link_libraries(calligra_filter_pdf2svg komain Poppler::Core) install(TARGETS calligra_filter_pdf2svg DESTINATION ${PLUGIN_INSTALL_DIR}/calligra/formatfilters) + +# pdf to odg +set(pdf2odg_PART_SRCS PdfImportDebug.cpp Pdf2OdgImport.cpp SvgOutputDev.cpp) + +add_library(calligra_filter_pdf2odg MODULE ${pdf2odg_PART_SRCS}) +calligra_filter_desktop_to_json(calligra_filter_pdf2odg calligra_filter_pdf2odg.desktop) + +target_link_libraries(calligra_filter_pdf2odg kopageapp karbonui Poppler::Core) + +install(TARGETS calligra_filter_pdf2odg DESTINATION ${PLUGIN_INSTALL_DIR}/calligra/formatfilters) diff --git a/filters/karbon/pdf/Pdf2OdgImport.h b/filters/karbon/pdf/Pdf2OdgImport.h new file mode 100644 --- /dev/null +++ b/filters/karbon/pdf/Pdf2OdgImport.h @@ -0,0 +1,52 @@ +/* This file is part of the KDE project + * Copyright (C) 2008 Jan Hambrecht + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef PDF2ODGIMPORT_H +#define PDF2ODGIMPORT_H + +#include +#include +#include + +class QFile; + +class KarbonDocument; +class KoXmlElement; +class KoShape; + +class Pdf2OdgImport : public KoFilter +{ + Q_OBJECT + +public: + Pdf2OdgImport(QObject* parent, const QVariantList&); + virtual ~Pdf2OdgImport(); + + virtual KoFilter::ConversionStatus convert(const QByteArray& from, const QByteArray& to); + + KoFilter::ConversionStatus convert(int pageNumber, QFile &in); + void convert(const KoXmlElement &rootElement, int pageNumber); + void buildDocument(const QList &toplevelShapes, const QList &shapes, int pageNumber); + +private: + KarbonDocument *m_document; +}; + +#endif + diff --git a/filters/karbon/pdf/Pdf2OdgImport.cpp b/filters/karbon/pdf/Pdf2OdgImport.cpp new file mode 100644 --- /dev/null +++ b/filters/karbon/pdf/Pdf2OdgImport.cpp @@ -0,0 +1,245 @@ +/* This file is part of the KDE project + * Copyright (C) 2008 Jan Hambrecht + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "Pdf2OdgImport.h" + +#include "PdfImportDebug.h" +#include "SvgOutputDev.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +// Don't show this warning: it's an issue in poppler +#ifdef __GNUC__ +#pragma GCC diagnostic ignored "-Wunused-parameter" +#endif + +// poppler includes +#include +#include + +K_PLUGIN_FACTORY_WITH_JSON(Pdf2OdgImportFactory, "calligra_filter_pdf2odg.json", + registerPlugin();) + +Pdf2OdgImport::Pdf2OdgImport(QObject*parent, const QVariantList&) + : KoFilter(parent) +{ + debugPdf << "PDF Import Filter"; +} + +Pdf2OdgImport::~Pdf2OdgImport() +{ +} + +KoFilter::ConversionStatus Pdf2OdgImport::convert(const QByteArray& from, const QByteArray& to) +{ + debugPdf << "to:" << to << " from:" << from; + + if (from != "application/pdf" || to != "application/vnd.oasis.opendocument.graphics") { + return KoFilter::NotImplemented; + } + debugPdf<inputFile(); + m_document = dynamic_cast(m_chain->outputDocument()); + if (!m_document) { + errorPdf<<"No KarbonDocument"<outputDocument(); + return KoFilter::CreationError; + } + Q_ASSERT(m_document->pages().isEmpty()); + + // read config file + globalParams = new GlobalParams(); + if (! globalParams) + return KoFilter::NotImplemented; + + GooString * fname = new GooString(QFile::encodeName(m_chain->inputFile()).data()); + PDFDoc * pdfDoc = new PDFDoc(fname, 0, 0, 0); + if (! pdfDoc) { + delete globalParams; + return KoFilter::StupidError; + } + + if (! pdfDoc->isOk()) { + delete globalParams; + delete pdfDoc; + return KoFilter::StupidError; + } + + double hDPI = 72.0; + double vDPI = 72.0; + + int firstPage = 1; + int lastPage = pdfDoc->getNumPages(); + + QString tmpPath = QDir::tempPath().append("/karbon"); + debugPdf << "converting pages" << firstPage << "-" << lastPage; + KoFilter::ConversionStatus status = KoFilter::OK; + for (int page = firstPage; status == KoFilter::OK && page <= lastPage; ++page) { + QTemporaryFile tmpFile(tmpPath); + if (!tmpFile.open()) { + status = KoFilter::CreationError; + break; + } + tmpFile.close(); + debugPdf<<"tmpFile:"<isOk()) { + int rotate = 0; + bool useMediaBox = true; + bool crop = false; + bool printing = false; + pdfDoc->displayPage(dev, page, hDPI, vDPI, rotate, useMediaBox, crop, printing); + dev->dumpContent(); + status = convert(page, tmpFile); + } + delete dev; + } + delete pdfDoc; + delete globalParams; + globalParams = 0; + return status; +} + +KoFilter::ConversionStatus Pdf2OdgImport::convert(int pageNumber, QFile &in) +{ + debugPdf<<"converting page:"<pages().count() < pageNumber) { + debugPdf<<"add page"<(m_document->pages(true).value(pageNumber-1)); + if (!mp) { + mp = new KoPAMasterPage(); + m_document->insertPage(mp, pageNumber-1); + } + m_document->insertPage(new KoPAPage(mp), pageNumber-1); + } + // Do the conversion! + convert(inputDoc.documentElement(), pageNumber); + + return KoFilter::OK; +} + +void Pdf2OdgImport::convert(const KoXmlElement &rootElement, int pageNumber) +{ + debugPdf<resourceManager()); + parser.setXmlBaseDir(QFileInfo(m_chain->inputFile()).filePath()); + + QList toplevelShapes = parser.parseSvg(rootElement, &pageSize); + // parse the root svg element + buildDocument(toplevelShapes, parser.shapes(), pageNumber); + + // set the page size + KoPageLayout & layout = m_document->pages().at(pageNumber-1)->pageLayout(); + layout.width = pageSize.width(); + layout.height = pageSize.height(); +} + +void Pdf2OdgImport::buildDocument(const QList &toplevelShapes, const QList &shapes, int pageNumber) +{ + Q_UNUSED(shapes); + KoPAPageBase *page = m_document->pages().at(pageNumber-1); + // if we have only top level groups, make them layers + bool onlyTopLevelGroups = true; + foreach(KoShape * shape, toplevelShapes) { + if (! dynamic_cast(shape) || shape->filterEffectStack()) { + onlyTopLevelGroups = false; + break; + } + } + KoShapeLayer *oldLayer = 0; + if (page->shapeCount()) { + oldLayer = dynamic_cast(page->shapes().first()); + } + if (onlyTopLevelGroups) { + foreach(KoShape * shape, toplevelShapes) { + // ungroup toplevel groups + KoShapeGroup *group = dynamic_cast(shape); + // NOTE: + // Only groups on first page has visible == true + // Maybe an issue with poppler? + group->setVisible(true); + QList children = group->shapes(); + KoShapeUngroupCommand cmd(group, children, QList() << group); + cmd.redo(); + + KoShapeLayer *layer = new KoShapeLayer(); + foreach(KoShape * child, children) { + layer->addShape(child); + } + if (!group->name().isEmpty()) { + layer->setName(group->name()); + } + layer->setVisible(group->isVisible()); + layer->setZIndex(group->zIndex()); + page->addShape(layer); + delete group; + } + } else { + KoShapeLayer *layer = new KoShapeLayer(); + foreach(KoShape * shape, toplevelShapes) { + shape->setVisible(true); + layer->addShape(shape); + } + layer->setVisible(true); + page->addShape(layer); + } + if (oldLayer) { + page->removeShape(oldLayer); + delete oldLayer; + } +} + +#include "Pdf2OdgImport.moc" diff --git a/filters/karbon/pdf/PdfImportDebug.cpp b/filters/karbon/pdf/PdfImportDebug.cpp --- a/filters/karbon/pdf/PdfImportDebug.cpp +++ b/filters/karbon/pdf/PdfImportDebug.cpp @@ -21,6 +21,6 @@ const QLoggingCategory &PDFIMPORT_LOG() { - static const QLoggingCategory category("calligra.filter.pdf2svg"); + static const QLoggingCategory category("calligra.filter.pdf"); return category; } diff --git a/filters/karbon/pdf/calligra_filter_pdf2odg.desktop b/filters/karbon/pdf/calligra_filter_pdf2odg.desktop new file mode 100644 --- /dev/null +++ b/filters/karbon/pdf/calligra_filter_pdf2odg.desktop @@ -0,0 +1,50 @@ +[Desktop Entry] +Name=Karbon PDF Import Filter +Name[bg]=Филтър за внасяне от PDF в Karbon +Name[bs]=Karbon PDF unosni Filter +Name[ca]=Filtre d'importació PDF per al Karbon +Name[ca@valencia]=Filtre d'importació PDF per al Karbon +Name[cs]=Importní filtr PDF pro Karbon +Name[da]=PDF-importfilter til Karbon +Name[de]=Karbon PDF-Importfilter +Name[el]=Φίλτρο εισαγωγής PDF του Karbon +Name[en_GB]=Karbon PDF Import Filter +Name[es]=Filtro de Karbon de importación de PDF +Name[et]=Karboni PDF-i impordifilter +Name[eu]=Karbon-en PDF inportazio-iragazkia +Name[fi]=Karbonin PDF-tuontisuodatin +Name[fr]=Filtre d'importation PDF de Karbon +Name[ga]=Scagaire Iompórtála Karbon PDF +Name[gl]=Filtro para Karbon de importación de PDF +Name[he]=מסנן ייבוא מ־PDF ל־Karbon +Name[hne]=कार्बन पीडीएफ आयात फिल्टर +Name[hu]=Karbon PDF importszűrő +Name[it]=Filtro di importazione PDF per Karbon +Name[ja]=Karbon PDF インポートフィルタ +Name[kk]=PDF -> Karbon импорт сүзгісі +Name[ko]=Karbon PDF 가져오기 필터 +Name[lv]=Karbon PDF importa filtrs +Name[mr]=कार्बन PDF आयात गाळणी +Name[nb]=PDF-importfilter for Karbon +Name[nds]=PDF-Importfilter för Karbon +Name[nl]=PDF-importfilter voor Karbon +Name[pl]=Filtr importu PDF do Karbon +Name[pt]=Filtro de Importação de PDF para o Karbon +Name[pt_BR]=Filtro de importação de PDF para Karbon +Name[ro]=Filtru importare Karbon pentru PDF +Name[ru]=Фильтр импорта файлов PDF в Karbon +Name[sk]=Importný filter PDF Karbon +Name[sl]=Uvozni filter PDF za Karbon +Name[sv]=Karbon PDF-importfilter +Name[tr]=Karbon PDF İçeriye Aktarma Filtresi +Name[uk]=Фільтр імпортування PDF до Karbon +Name[wa]=Passete d' abagaedje PDF di Karbon +Name[x-test]=xxKarbon PDF Import Filterxx +Name[zh_CN]=Karbon PDF 导入过滤器 +Name[zh_TW]=Karbon PDF 匯入過濾程式 +X-KDE-ServiceTypes=Calligra/Filter +Type=Service +X-KDE-Export=application/vnd.oasis.opendocument.graphics +X-KDE-Import=application/pdf +X-KDE-Library=calligra_filter_pdf2odg +X-KDE-Weight=1