Kaydet (Commit) 7939efc7 authored tarafından Ashod Nakashian's avatar Ashod Nakashian Kaydeden (comit) Jan Holesovsky

sd: import PDFs as images using Pdfium new SdPdfFilter

LOK now opens PDFs as images using Pdfium,
which has a superior accuracy and support
to poppler, the default pdf reader.

Change-Id: Ifbbecf7f048f001836fb98886705cba47e6bed4e
üst 9b11fdd3
......@@ -231,10 +231,14 @@ public:
const VectorGraphicDataPtr& getVectorGraphicData() const;
void setPdfData(const std::shared_ptr<css::uno::Sequence<sal_Int8>>& rPdfData);
void setPdfData(const css::uno::Sequence<sal_Int8>& rPdfData) { setPdfData(std::make_shared<css::uno::Sequence<sal_Int8>>(rPdfData)); }
std::shared_ptr<css::uno::Sequence<sal_Int8>> getPdfData() const;
bool hasPdfData() const;
/// Set the page number of the multi-page source this Graphic is rendered from.
void setPageNumber(sal_Int32 nPageNumber);
/// Get the page number of the multi-page source this Graphic is rendered from.
sal_Int32 getPageNumber() const;
static css::uno::Sequence<sal_Int8> getUnoTunnelId();
};
......
......@@ -36,6 +36,9 @@ VCL_DLLPUBLIC bool ImportPDF(SvStream& rStream, Bitmap& rBitmap,
sal_uInt64 nPos = STREAM_SEEK_TO_BEGIN,
sal_uInt64 nSize = STREAM_SEEK_TO_END);
VCL_DLLPUBLIC bool ImportPDF(SvStream& rStream, Graphic& rGraphic);
VCL_DLLPUBLIC size_t ImportPDF(const OUString& rURL, std::vector<Bitmap>& rBitmaps,
css::uno::Sequence<sal_Int8>& rPdfData);
}
#endif // INCLUDED_VCL_SOURCE_FILTER_IPDF_PDFREAD_HXX
......
......@@ -154,6 +154,7 @@ $(eval $(call gb_Library_add_exception_objects,sd,\
sd/source/filter/html/buttonset \
sd/source/filter/html/htmlex \
sd/source/filter/html/sdhtmlfilter \
sd/source/filter/pdf/sdpdffilter \
sd/source/filter/sdfilter \
sd/source/filter/sdpptwrp \
sd/source/filter/xml/sdtransform \
......
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#ifndef INCLUDED_SD_INC_SDPDFIUMFILTER_HXX
#define INCLUDED_SD_INC_SDPDFIUMFILTER_HXX
#include <com/sun/star/drawing/XShape.hpp>
#include "sdfilter.hxx"
class SdPdfFilter : public SdFilter
{
public:
SdPdfFilter(SfxMedium& rMedium, ::sd::DrawDocShell& rDocShell);
virtual ~SdPdfFilter() override;
bool Import();
bool Export() override;
};
#endif // INCLUDED_SD_INC_SDGRFFILTER_HXX
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <com/sun/star/drawing/GraphicExportFilter.hpp>
#include <com/sun/star/graphic/GraphicProvider.hpp>
#include <com/sun/star/graphic/XGraphicProvider.hpp>
#include <com/sun/star/graphic/GraphicType.hpp>
#include <com/sun/star/ucb/SimpleFileAccess.hpp>
#include <com/sun/star/ucb/XSimpleFileAccess2.hpp>
#include <unotools/localfilehelper.hxx>
#include <vcl/errinf.hxx>
#include <vcl/weld.hxx>
#include <vcl/metaact.hxx>
#include <vcl/virdev.hxx>
#include <sfx2/sfxsids.hrc>
#include <sfx2/docfile.hxx>
#include <sfx2/docfilt.hxx>
#include <sfx2/frame.hxx>
#include <svx/svdograf.hxx>
#include <svx/svdpagv.hxx>
#include <strings.hrc>
#include <DrawViewShell.hxx>
#include <DrawDocShell.hxx>
#include <ClientView.hxx>
#include <FrameView.hxx>
#include <comphelper/anytostring.hxx>
#include <cppuhelper/exc_hlp.hxx>
#include <comphelper/processfactory.hxx>
#include <unotools/pathoptions.hxx>
#include <sfx2/filedlghelper.hxx>
#include <vcl/graphicfilter.hxx>
#include <svx/xoutbmp.hxx>
#include <sdpage.hxx>
#include <drawdoc.hxx>
#include <sdresid.hxx>
#include <sdpdffilter.hxx>
#include <ViewShellBase.hxx>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/beans/PropertyValues.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/document/XFilter.hpp>
#include <com/sun/star/document/XExporter.hpp>
#include <com/sun/star/view/XSelectionSupplier.hpp>
#include <com/sun/star/drawing/XDrawView.hpp>
#include <DrawController.hxx>
#include <cppuhelper/implbase.hxx>
#include <com/sun/star/drawing/XShape.hpp>
#include <com/sun/star/task/XInteractionHandler.hpp>
#include <com/sun/star/task/XInteractionRequest.hpp>
#include <com/sun/star/drawing/GraphicFilterRequest.hpp>
#include <vcl/bitmap.hxx>
#include <vcl/graph.hxx>
#include <vcl/pdfread.hxx>
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::graphic;
using namespace ::com::sun::star::io;
using namespace ::com::sun::star::ucb;
using namespace com::sun::star::ui::dialogs;
using namespace ::sfx2;
SdPdfFilter::SdPdfFilter(SfxMedium& rMedium, ::sd::DrawDocShell& rDocShell)
: SdFilter(rMedium, rDocShell)
{
}
SdPdfFilter::~SdPdfFilter() {}
bool SdPdfFilter::Import()
{
//FIXME: Replace with parsing the PDF elements to allow editing.
//FIXME: For now we import as images for simplicity.
const OUString aFileName(
mrMedium.GetURLObject().GetMainURL(INetURLObject::DecodeMechanism::NONE));
uno::Sequence<sal_Int8> aPdfData;
std::vector<Bitmap> aBitmaps;
if (vcl::ImportPDF(aFileName, aBitmaps, aPdfData) == 0)
return false;
// Prepare the link with the PDF stream.
const size_t nGraphicContentSize = aPdfData.getLength();
std::unique_ptr<sal_uInt8[]> pGraphicContent(new sal_uInt8[nGraphicContentSize]);
memcpy(pGraphicContent.get(), aPdfData.get(), nGraphicContentSize);
GfxLink aGfxLink(std::move(pGraphicContent), nGraphicContentSize, GfxLinkType::NativePdf);
mrDocument.CreateFirstPages();
for (int i = 0; i < aBitmaps.size() - 1; ++i)
{
mrDocument.DuplicatePage(0);
}
size_t nPageNumber = 0;
for (Bitmap& aBitmap : aBitmaps)
{
// Create the Graphic and link the original PDF stream.
Graphic aGraphic(aBitmap);
aGraphic.setPdfData(std::make_shared<uno::Sequence<sal_Int8>>(aPdfData));
aGraphic.SetLink(aGfxLink);
// Create the page and insert the Graphic.
SdPage* pPage = mrDocument.GetSdPage(nPageNumber++, PageKind::Standard);
Point aPos;
Size aPagSize(pPage->GetSize());
Size aGrfSize(OutputDevice::LogicToLogic(aGraphic.GetPrefSize(), aGraphic.GetPrefMapMode(),
MapMode(MapUnit::Map100thMM)));
aPagSize.AdjustWidth(-(pPage->GetLeftBorder() + pPage->GetRightBorder()));
aPagSize.AdjustHeight(-(pPage->GetUpperBorder() + pPage->GetLowerBorder()));
// scale to fit page
if (((aGrfSize.Height() > aPagSize.Height()) || (aGrfSize.Width() > aPagSize.Width()))
&& aGrfSize.Height() && aPagSize.Height())
{
double fGrfWH = static_cast<double>(aGrfSize.Width()) / aGrfSize.Height();
double fWinWH = static_cast<double>(aPagSize.Width()) / aPagSize.Height();
// adjust graphic to page size (scales)
if (fGrfWH < fWinWH)
{
aGrfSize.setWidth(static_cast<long>(aPagSize.Height() * fGrfWH));
aGrfSize.setHeight(aPagSize.Height());
}
else if (fGrfWH > 0.F)
{
aGrfSize.setWidth(aPagSize.Width());
aGrfSize.setHeight(static_cast<long>(aPagSize.Width() / fGrfWH));
}
}
// set output rectangle for graphic
aPos.setX(((aPagSize.Width() - aGrfSize.Width()) >> 1) + pPage->GetLeftBorder());
aPos.setY(((aPagSize.Height() - aGrfSize.Height()) >> 1) + pPage->GetUpperBorder());
pPage->InsertObject(new SdrGrafObj(pPage->getSdrModelFromSdrPage(), aGraphic,
::tools::Rectangle(aPos, aGrfSize)));
}
return true;
}
bool SdPdfFilter::Export() { return false; }
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
......@@ -79,6 +79,7 @@
#include <sdcgmfilter.hxx>
#include <sdgrffilter.hxx>
#include <sdhtmlfilter.hxx>
#include <sdpdffilter.hxx>
#include <framework/FrameworkHelper.hxx>
#include <SdUnoDrawView.hxx>
......@@ -489,12 +490,18 @@ bool DrawDocShell::ConvertFrom( SfxMedium& rMedium )
ErrCode nError = ERRCODE_NONE;
bRet = SdXMLFilter( rMedium, *this, SDXMLMODE_Normal, SOFFICE_FILEFORMAT_60 ).Import( nError );
}
else if( aFilterName == "CGM - Computer Graphics Metafile" )
else if (aFilterName == "CGM - Computer Graphics Metafile")
{
mpDoc->CreateFirstPages();
mpDoc->StopWorkStartupDelay();
bRet = SdCGMFilter( rMedium, *this ).Import();
}
else if (aFilterName == "draw_pdf_import")
{
mpDoc->CreateFirstPages();
mpDoc->StopWorkStartupDelay();
bRet = SdPdfFilter(rMedium, *this).Import();
}
else
{
mpDoc->CreateFirstPages();
......
......@@ -30,6 +30,7 @@ $(eval $(call gb_CppunitTest_use_libraries,sdext_pdfimport,\
sal \
test \
unotest \
tl \
xo \
))
......
......@@ -30,6 +30,7 @@ $(eval $(call gb_Executable_use_libraries,pdf2xml,\
unotest \
cppuhelper \
sal \
tl \
xo \
))
......
......@@ -28,6 +28,7 @@ $(eval $(call gb_Executable_use_libraries,pdfunzip,\
cppuhelper \
sal \
vcl \
tl \
xo \
))
......
......@@ -27,6 +27,7 @@ $(eval $(call gb_Library_use_libraries,pdfimport,\
cppu \
cppuhelper \
sal \
tl \
xo \
))
......
......@@ -35,6 +35,7 @@
#include <rtl/strbuf.hxx>
#include <rtl/byteseq.hxx>
#include <comphelper/lok.hxx>
#include <comphelper/propertysequence.hxx>
#include <cppuhelper/exc_hlp.hxx>
#include <com/sun/star/io/XInputStream.hpp>
......@@ -71,6 +72,12 @@
#include <rtl/character.hxx>
#include <vcl/bitmapaccess.hxx>
#include <vcl/bitmap.hxx>
#include <vcl/graph.hxx>
#include <vcl/pdfread.hxx>
#include <vcl/pngwrite.hxx>
using namespace com::sun::star;
namespace pdfi
......@@ -1001,20 +1008,12 @@ public:
}
};
bool xpdf_ImportFromFile_Poppler(const OUString& aSysUPath,
const ContentSinkSharedPtr& rSink,
const uno::Reference<task::XInteractionHandler>& xIHdl,
const bool bIsEncrypted,
const OUString& aPwd,
const uno::Reference<uno::XComponentContext>& xContext,
const OUString& rFilterOptions);
bool xpdf_ImportFromFile( const OUString& rURL,
const ContentSinkSharedPtr& rSink,
const uno::Reference< task::XInteractionHandler >& xIHdl,
const OUString& rPwd,
const uno::Reference< uno::XComponentContext >& xContext,
const OUString& rFilterOptions )
bool xpdf_ImportFromFile(const OUString& rURL,
const ContentSinkSharedPtr& rSink,
const uno::Reference<task::XInteractionHandler>& xIHdl,
const OUString& rPwd,
const uno::Reference<uno::XComponentContext>& xContext,
const OUString& rFilterOptions)
{
OSL_ASSERT(rSink);
......@@ -1039,19 +1038,6 @@ bool xpdf_ImportFromFile( const OUString& rURL,
return false;
}
return xpdf_ImportFromFile_Poppler(aSysUPath, rSink, xIHdl, bIsEncrypted, aPwd, xContext, rFilterOptions);
}
/// Parse PDf file using libpoppler, which is quite limited
/// to be phased out in favor of pdfium.
bool xpdf_ImportFromFile_Poppler(const OUString& aSysUPath,
const ContentSinkSharedPtr& rSink,
const uno::Reference<task::XInteractionHandler>& /*xIHdl*/,
const bool bIsEncrypted,
const OUString& aPwd,
const uno::Reference<uno::XComponentContext>& xContext,
const OUString& rFilterOptions)
{
// Determine xpdfimport executable URL:
OUString converterURL("$BRAND_BASE_DIR/" LIBO_BIN_FOLDER "/xpdfimport");
rtl::Bootstrap::expandMacros(converterURL); //TODO: detect failure
......
......@@ -101,6 +101,7 @@
#include <officecfg/Office/Common.hxx>
#include <osl/file.hxx>
#include <comphelper/scopeguard.hxx>
#include <comphelper/lok.hxx>
#include <sfx2/signaturestate.hxx>
#include <sfx2/app.hxx>
......@@ -746,9 +747,13 @@ bool SfxObjectShell::DoLoad( SfxMedium *pMed )
if ( GetError() == ERRCODE_NONE )
{
// Experimental PDF importing using PDFium. This is currently enabled for LOK only and
// we handle it not via XmlFilterAdaptor but a new SdPdfFiler.
const bool bPdfiumImport = comphelper::LibreOfficeKit::isActive() && pMedium->GetFilter() &&
(pMedium->GetFilter()->GetFilterName() == "draw_pdf_import");
pImpl->nLoadedFlags = SfxLoadedFlags::NONE;
pImpl->bModelInitialized = false;
if ( pMedium->GetFilter() && ( pMedium->GetFilter()->GetFilterFlags() & SfxFilterFlags::STARONEFILTER ) )
if ( pMedium->GetFilter() && ( pMedium->GetFilter()->GetFilterFlags() & SfxFilterFlags::STARONEFILTER ) && !bPdfiumImport )
{
uno::Reference < beans::XPropertySet > xSet( GetModel(), uno::UNO_QUERY );
const OUString sLockUpdates("LockUpdates");
......
......@@ -777,6 +777,7 @@ OUString SvXMLGraphicHelper::implSaveGraphic(css::uno::Reference<css::graphic::X
{
pStream->WriteBytes(aGfxLink.GetData(), aGfxLink.GetDataSize());
}
rOutSavedMimeType = aMimeType;
bSuccess = (pStream->GetError() == ERRCODE_NONE);
}
......
......@@ -88,10 +88,19 @@ private:
bool mbSwapOut;
bool mbDummyContext;
VectorGraphicDataPtr maVectorGraphicData;
/// The PDF stream from which this Graphic is rendered,
/// as converted (version downgraded) from the original,
/// which should be in GfxLink.
std::shared_ptr<css::uno::Sequence<sal_Int8>> mpPdfData;
std::unique_ptr<GraphicID> mpGraphicID;
GraphicExternalLink maGraphicExternalLink;
/// Used with GfxLink and/or PdfData when they store original media
/// which might be multi-page (PDF, f.e.) and we need to re-render
/// this Graphic (a page) from the source in GfxLink or PdfData.
sal_Int32 mnPageNumber;
std::chrono::high_resolution_clock::time_point maLastUsed;
bool mbPrepared;
......
......@@ -44,7 +44,7 @@
#include <vcl/wmf.hxx>
#include <vcl/settings.hxx>
#include "igif/gifread.hxx"
#include <pdfread.hxx>
#include <vcl/pdfread.hxx>
#include "jpeg/jpeg.hxx"
#include "ixbm/xbmread.hxx"
#include "ixpm/xpmread.hxx"
......
......@@ -7,7 +7,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <pdfread.hxx>
#include <vcl/pdfread.hxx>
#include <config_features.h>
......@@ -19,6 +19,7 @@
#include <vcl/graph.hxx>
#include <bitmapwriteaccess.hxx>
#include <unotools/ucbstreamhelper.hxx>
using namespace com::sun::star;
......@@ -237,9 +238,31 @@ bool ImportPDF(SvStream& rStream, Graphic& rGraphic)
Bitmap aBitmap;
const bool bRet = ImportPDF(rStream, aBitmap, aPdfData);
rGraphic = aBitmap;
rGraphic.setPdfData(aPdfData);
rGraphic.setPdfData(std::make_shared<css::uno::Sequence<sal_Int8>>(aPdfData));
return bRet;
}
size_t ImportPDF(const OUString& rURL, std::vector<Bitmap>& rBitmaps,
css::uno::Sequence<sal_Int8>& rPdfData)
{
std::unique_ptr<SvStream> xStream(
::utl::UcbStreamHelper::CreateStream(rURL, StreamMode::READ | StreamMode::SHARE_DENYNONE));
if (generatePreview(*xStream, rBitmaps, STREAM_SEEK_TO_BEGIN, STREAM_SEEK_TO_END, 0, -1) == 0)
return 0;
// Save the original PDF stream for later use.
SvMemoryStream aMemoryStream;
if (!getCompatibleStream(*xStream, aMemoryStream, STREAM_SEEK_TO_BEGIN, STREAM_SEEK_TO_END))
return 0;
aMemoryStream.Seek(STREAM_SEEK_TO_END);
rPdfData = css::uno::Sequence<sal_Int8>(aMemoryStream.Tell());
aMemoryStream.Seek(STREAM_SEEK_TO_BEGIN);
aMemoryStream.ReadBytes(rPdfData.getArray(), rPdfData.getLength());
return rBitmaps.size();
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
......@@ -574,6 +574,16 @@ bool Graphic::hasPdfData() const
return pPdfData && pPdfData->hasElements();
}
void Graphic::setPageNumber(sal_Int32 nPageNumber)
{
mxImpGraphic->mnPageNumber = nPageNumber;
}
sal_Int32 Graphic::getPageNumber() const
{
return mxImpGraphic->mnPageNumber;
}
OUString Graphic::getOriginURL() const
{
if (mxImpGraphic)
......
......@@ -43,7 +43,7 @@
#include <o3tl/make_unique.hxx>
#include <vcl/gdimetafiletools.hxx>
#include <pdfread.hxx>
#include <vcl/pdfread.hxx>
#define GRAPHIC_MTFTOBMP_MAXEXT 2048
#define GRAPHIC_STREAMBUFSIZE 8192UL
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment