LibreOffice Module filter (master) 1
pdffilter.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include "pdffilter.hxx"
21#include "pdfexport.hxx"
23#include <vcl/svapp.hxx>
24#include <vcl/window.hxx>
25#include <svl/outstrm.hxx>
27#include <unotools/tempfile.hxx>
30#include <memory>
31
32#include <com/sun/star/io/XOutputStream.hpp>
33
36
37#include <boost/property_tree/json_parser/error.hpp>
38
39using namespace ::com::sun::star::io;
40
41PDFFilter::PDFFilter( const Reference< XComponentContext > &rxContext ) :
42 mxContext( rxContext )
43{
44}
45
46
48{
49}
50
51
52bool PDFFilter::implExport( const Sequence< PropertyValue >& rDescriptor )
53{
54 Reference< XOutputStream > xOStm;
55 Sequence< PropertyValue > aFilterData;
56 OUString aFilterOptions;
57 sal_Int32 nLength = rDescriptor.getLength();
58 const PropertyValue* pValue = rDescriptor.getConstArray();
59 bool bIsRedactMode = false;
60 bool bRet = false;
61 Reference< task::XStatusIndicator > xStatusIndicator;
62 Reference< task::XInteractionHandler > xIH;
63
64 for (sal_Int32 i = 0; i < nLength; ++i)
65 {
66 if ( pValue[ i ].Name == "OutputStream" )
67 pValue[ i ].Value >>= xOStm;
68 else if ( pValue[ i ].Name == "FilterData" )
69 pValue[ i ].Value >>= aFilterData;
70 else if ( pValue[ i ].Name == "FilterOptions" )
71 pValue[ i ].Value >>= aFilterOptions;
72 else if ( pValue[ i ].Name == "StatusIndicator" )
73 pValue[ i ].Value >>= xStatusIndicator;
74 else if ( pValue[i].Name == "InteractionHandler" )
75 pValue[i].Value >>= xIH;
76 }
77
78 for (sal_Int32 i = 0 ; i < nLength; ++i)
79 {
80 if ( pValue[i].Name == "IsRedactMode")
81 pValue[i].Value >>= bIsRedactMode;
82 }
83
84 if (!aFilterData.hasElements() && aFilterOptions.startsWith("{"))
85 {
86 try
87 {
88 // Allow setting filter data keys from the cmdline.
89 std::vector<PropertyValue> aData
90 = comphelper::JsonToPropertyValues(aFilterOptions.toUtf8());
92 }
93 catch (const boost::property_tree::json_parser::json_parser_error& e)
94 {
95 // This wasn't a valid json; maybe came from import filter (tdf#150846)
96 SAL_WARN("filter.pdf", "error parsing FilterOptions: " << e.message());
97 }
98 }
99
100 /* we don't get FilterData if we are exporting directly
101 to pdf, but we have to use the last user settings (especially for the CompressMode) */
102 if ( !aFilterData.hasElements() )
103 {
104 FilterConfigItem aCfgItem( u"Office.Common/Filter/PDF/Export/" );
105 aCfgItem.ReadBool( "UseLosslessCompression", false );
106 aCfgItem.ReadInt32( "Quality", 90 );
107 aCfgItem.ReadBool( "ReduceImageResolution", false );
108 aCfgItem.ReadInt32( "MaxImageResolution", 300 );
109 aCfgItem.ReadBool( "UseTaggedPDF", false );
110 aCfgItem.ReadInt32( "SelectPdfVersion", 0 );
111 aCfgItem.ReadBool("PDFUACompliance", false);
112 aCfgItem.ReadBool( "ExportNotes", false );
113 aCfgItem.ReadBool( "ExportNotesInMargin", false );
114 aCfgItem.ReadBool( "ExportPlaceholders", false );
115 aCfgItem.ReadBool( "ExportNotesPages", false );
116 aCfgItem.ReadBool( "ExportOnlyNotesPages", false );
117 aCfgItem.ReadBool( "UseTransitionEffects", true );
118 aCfgItem.ReadBool( "IsSkipEmptyPages", false );
119 aCfgItem.ReadBool( "ExportFormFields", true );
120 aCfgItem.ReadInt32( "FormsType", 0 );
121 aCfgItem.ReadBool( "HideViewerToolbar", false );
122 aCfgItem.ReadBool( "HideViewerMenubar", false );
123 aCfgItem.ReadBool( "HideViewerWindowControls", false );
124 aCfgItem.ReadBool( "ResizeWindowToInitialPage", false );
125 aCfgItem.ReadBool( "CenterWindow", false );
126 aCfgItem.ReadBool( "OpenInFullScreenMode", false );
127 aCfgItem.ReadBool( "DisplayPDFDocumentTitle", true );
128 aCfgItem.ReadInt32( "InitialView", 0 );
129 aCfgItem.ReadInt32( "Magnification", 0 );
130 aCfgItem.ReadInt32( "Zoom", 100 );
131 aCfgItem.ReadInt32( "PageLayout", 0 );
132 aCfgItem.ReadBool( "FirstPageOnLeft", false );
133 aCfgItem.ReadInt32( "InitialPage", 1 );
134 aCfgItem.ReadBool( "IsAddStream", false );
135
136 // the encryption is not available when exporting directly, since the encryption is off by default and the selection
137 // (encrypt or not) is not persistent; it's available through macro though,
138 // provided the correct property values are set, see help
139
140 // now, the relative link stuff
141 aCfgItem.ReadBool( "ExportLinksRelativeFsys", false );
142 aCfgItem.ReadInt32("PDFViewSelection", 0 );
143 aCfgItem.ReadBool( "ConvertOOoTargetToPDFTarget", false );
144 aCfgItem.ReadBool( "ExportBookmarksToPDFDestination", false );
145
146 aCfgItem.ReadBool( "ExportBookmarks", true );
147 aCfgItem.ReadBool( "ExportHiddenSlides", false );
148 aCfgItem.ReadBool( "SinglePageSheets", false );
149 aCfgItem.ReadInt32( "OpenBookmarkLevels", -1 );
150
151 aCfgItem.ReadBool( "IsRedactMode", false);
152
153 aFilterData = aCfgItem.GetFilterData();
154 }
155
156
157 if (bIsRedactMode)
158 {
159 bool bFound = false;
160
161 for (PropertyValue& rProp : asNonConstRange(aFilterData))
162 {
163 if (rProp.Name == "IsRedactMode")
164 {
165 rProp.Value <<= bIsRedactMode;
166 bFound = true;
167 break;
168 }
169 }
170
171 if (!bFound)
172 {
173 sal_Int32 nNewSize = aFilterData.getLength() + 1;
174 aFilterData.realloc( nNewSize );
175 auto pFilterData = aFilterData.getArray();
176 pFilterData[nNewSize - 1].Name = "IsRedactMode";
177 pFilterData[nNewSize - 1].Value <<= bIsRedactMode;
178 }
179 }
180
181 if( mxSrcDoc.is() && xOStm.is() )
182 {
183 PDFExport aExport( mxSrcDoc, xStatusIndicator, xIH, mxContext );
184 ::utl::TempFileNamed aTempFile;
185
186 aTempFile.EnableKillingFile();
187 bRet = aExport.Export( aTempFile.GetURL(), aFilterData );
188
189 if( bRet )
190 {
191 std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( aTempFile.GetURL(), StreamMode::READ ));
192
193 if( pIStm )
194 {
195 SvOutputStream aOStm( xOStm );
196
197 aOStm.WriteStream( *pIStm );
198 bRet = ( aOStm.Tell() && ( aOStm.GetError() == ERRCODE_NONE ) );
199 }
200 }
201 }
202
203 return bRet;
204}
205
206namespace {
207
208class FocusWindowWaitCursor
209{
210private:
211
212 VclPtr<vcl::Window> m_pFocusWindow;
213
214public:
215 FocusWindowWaitCursor() :
216 m_pFocusWindow( Application::GetFocusWindow() )
217 {
218 if( m_pFocusWindow )
219 {
220 m_pFocusWindow->AddEventListener( LINK( this, FocusWindowWaitCursor, DestroyedLink ) );
221 m_pFocusWindow->EnterWait();
222 }
223 }
224
225 ~FocusWindowWaitCursor()
226 {
227 if( m_pFocusWindow )
228 {
229 m_pFocusWindow->LeaveWait();
230 m_pFocusWindow->RemoveEventListener( LINK( this, FocusWindowWaitCursor, DestroyedLink ) );
231 }
232 }
233
234 DECL_LINK( DestroyedLink, VclWindowEvent&, void );
235};
236
237}
238
239IMPL_LINK( FocusWindowWaitCursor, DestroyedLink, VclWindowEvent&, rEvent, void )
240{
241 if( rEvent.GetId() == VclEventId::ObjectDying )
242 m_pFocusWindow = nullptr;
243}
244
245
246sal_Bool SAL_CALL PDFFilter::filter( const Sequence< PropertyValue >& rDescriptor )
247{
248 FocusWindowWaitCursor aCur;
249
251
252 const bool bRet = implExport( rDescriptor );
253
255
256 return bRet;
257}
258
259
260void SAL_CALL PDFFilter::cancel( )
261{
262}
263
264
265void SAL_CALL PDFFilter::setSourceDocument( const Reference< XComponent >& xDoc )
266{
267 mxSrcDoc = xDoc;
268}
269
270
271void SAL_CALL PDFFilter::initialize( const css::uno::Sequence< css::uno::Any >& )
272{
273}
274
275
277{
278 return "com.sun.star.comp.PDF.PDFFilter";
279}
280
281
282sal_Bool SAL_CALL PDFFilter::supportsService( const OUString& rServiceName )
283{
284 return cppu::supportsService( this, rServiceName );
285}
286
287
288css::uno::Sequence< OUString > SAL_CALL PDFFilter::getSupportedServiceNames( )
289{
290 return { "com.sun.star.document.PDFFilter" };
291}
292
293
294extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
296 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
297{
298 return cppu::acquire(new PDFFilter(context));
299}
300
301/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool ReadBool(const OUString &rKey, bool bDefault)
sal_Int32 ReadInt32(const OUString &rKey, sal_Int32 nDefault)
const css::uno::Sequence< css::beans::PropertyValue > & GetFilterData() const
bool Export(const OUString &rFile, const css::uno::Sequence< css::beans::PropertyValue > &rFilterData)
Definition: pdfexport.cxx:423
virtual ~PDFFilter() override
Definition: pdffilter.cxx:47
virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: pdffilter.cxx:288
bool implExport(const Sequence< PropertyValue > &rDescriptor)
Definition: pdffilter.cxx:52
virtual void SAL_CALL initialize(const Sequence< Any > &aArguments) override
Definition: pdffilter.cxx:271
Reference< XComponent > mxSrcDoc
Definition: pdffilter.hxx:42
virtual void SAL_CALL setSourceDocument(const Reference< XComponent > &xDoc) override
Definition: pdffilter.cxx:265
virtual sal_Bool SAL_CALL filter(const Sequence< PropertyValue > &rDescriptor) override
Definition: pdffilter.cxx:246
virtual OUString SAL_CALL getImplementationName() override
Definition: pdffilter.cxx:276
virtual void SAL_CALL cancel() override
Definition: pdffilter.cxx:260
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: pdffilter.cxx:282
PDFFilter(const Reference< XComponentContext > &rxContext)
Definition: pdffilter.cxx:41
Reference< XComponentContext > mxContext
Definition: pdffilter.hxx:41
void SetCacheGlyphsWhenDoingFallbackFonts(bool bOK)
static SalLayoutGlyphsCache * self()
void EnableKillingFile(bool bEnable=true)
OUString const & GetURL() const
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
DECL_LINK(CheckNameHdl, SvxNameDialog &, bool)
uno::Reference< uno::XComponentContext > mxContext
float u
#define ERRCODE_NONE
#define SAL_WARN(area, stream)
constexpr OUStringLiteral aData
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
std::vector< css::beans::PropertyValue > JsonToPropertyValues(const OString &rJson)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
Definition: gentoken.py:48
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * filter_PDFFilter_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: pdffilter.cxx:295
IMPL_LINK(FocusWindowWaitCursor, DestroyedLink, VclWindowEvent &, rEvent, void)
Definition: pdffilter.cxx:239
OUString Name
unsigned char sal_Bool
sal_Int32 nLength