1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "pdffilter.hxx"
21 #include "pdfexport.hxx"
22 #include <cppuhelper/supportsservice.hxx>
23 #include <vcl/svapp.hxx>
24 #include <vcl/window.hxx>
25 #include <svl/outstrm.hxx>
26 #include <unotools/ucbstreamhelper.hxx>
27 #include <unotools/tempfile.hxx>
28 #include <vcl/FilterConfigItem.hxx>
29 #include <vcl/glyphitemcache.hxx>
32 #include <com/sun/star/io/XOutputStream.hpp>
34 #include <comphelper/propertysequence.hxx>
35 #include <comphelper/sequence.hxx>
37 #include <boost/property_tree/json_parser/error.hpp>
39 using namespace ::com::sun::star::io
;
41 PDFFilter::PDFFilter( const Reference
< XComponentContext
> &rxContext
) :
42 mxContext( rxContext
)
47 PDFFilter::~PDFFilter()
52 bool PDFFilter::implExport( const Sequence
< PropertyValue
>& rDescriptor
)
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;
61 Reference
< task::XStatusIndicator
> xStatusIndicator
;
62 Reference
< task::XInteractionHandler
> xIH
;
64 for (sal_Int32 i
= 0; i
< nLength
; ++i
)
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
;
78 for (sal_Int32 i
= 0 ; i
< nLength
; ++i
)
80 if ( pValue
[i
].Name
== "IsRedactMode")
81 pValue
[i
].Value
>>= bIsRedactMode
;
84 if (!aFilterData
.hasElements() && aFilterOptions
.startsWith("{"))
88 // Allow setting filter data keys from the cmdline.
89 std::vector
<PropertyValue
> aData
90 = comphelper::JsonToPropertyValues(aFilterOptions
.toUtf8());
91 aFilterData
= comphelper::containerToSequence(aData
);
93 catch (const boost::property_tree::json_parser::json_parser_error
& e
)
95 // This wasn't a valid json; maybe came from import filter (tdf#150846)
96 SAL_WARN("filter.pdf", "error parsing FilterOptions: " << e
.message());
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() )
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 );
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
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 );
146 aCfgItem
.ReadBool( "ExportBookmarks", true );
147 aCfgItem
.ReadBool( "ExportHiddenSlides", false );
148 aCfgItem
.ReadBool( "SinglePageSheets", false );
149 aCfgItem
.ReadInt32( "OpenBookmarkLevels", -1 );
151 aCfgItem
.ReadBool( "IsRedactMode", false);
153 aFilterData
= aCfgItem
.GetFilterData();
161 for (PropertyValue
& rProp
: asNonConstRange(aFilterData
))
163 if (rProp
.Name
== "IsRedactMode")
165 rProp
.Value
<<= bIsRedactMode
;
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
;
181 if( mxSrcDoc
.is() && xOStm
.is() )
183 PDFExport
aExport( mxSrcDoc
, xStatusIndicator
, xIH
, mxContext
);
184 ::utl::TempFileNamed aTempFile
;
186 aTempFile
.EnableKillingFile();
187 bRet
= aExport
.Export( aTempFile
.GetURL(), aFilterData
);
191 std::unique_ptr
<SvStream
> pIStm(::utl::UcbStreamHelper::CreateStream( aTempFile
.GetURL(), StreamMode::READ
));
195 SvOutputStream
aOStm( xOStm
);
197 aOStm
.WriteStream( *pIStm
);
198 bRet
= ( aOStm
.Tell() && ( aOStm
.GetError() == ERRCODE_NONE
) );
208 class FocusWindowWaitCursor
212 VclPtr
<vcl::Window
> m_pFocusWindow
;
215 FocusWindowWaitCursor() :
216 m_pFocusWindow( Application::GetFocusWindow() )
220 m_pFocusWindow
->AddEventListener( LINK( this, FocusWindowWaitCursor
, DestroyedLink
) );
221 m_pFocusWindow
->EnterWait();
225 ~FocusWindowWaitCursor()
229 m_pFocusWindow
->LeaveWait();
230 m_pFocusWindow
->RemoveEventListener( LINK( this, FocusWindowWaitCursor
, DestroyedLink
) );
234 DECL_LINK( DestroyedLink
, VclWindowEvent
&, void );
239 IMPL_LINK( FocusWindowWaitCursor
, DestroyedLink
, VclWindowEvent
&, rEvent
, void )
241 if( rEvent
.GetId() == VclEventId::ObjectDying
)
242 m_pFocusWindow
= nullptr;
246 sal_Bool SAL_CALL
PDFFilter::filter( const Sequence
< PropertyValue
>& rDescriptor
)
248 FocusWindowWaitCursor aCur
;
250 SalLayoutGlyphsCache::self()->SetCacheGlyphsWhenDoingFallbackFonts(true);
252 const bool bRet
= implExport( rDescriptor
);
254 SalLayoutGlyphsCache::self()->SetCacheGlyphsWhenDoingFallbackFonts(false);
260 void SAL_CALL
PDFFilter::cancel( )
265 void SAL_CALL
PDFFilter::setSourceDocument( const Reference
< XComponent
>& xDoc
)
271 void SAL_CALL
PDFFilter::initialize( const css::uno::Sequence
< css::uno::Any
>& )
276 OUString SAL_CALL
PDFFilter::getImplementationName()
278 return "com.sun.star.comp.PDF.PDFFilter";
282 sal_Bool SAL_CALL
PDFFilter::supportsService( const OUString
& rServiceName
)
284 return cppu::supportsService( this, rServiceName
);
288 css::uno::Sequence
< OUString
> SAL_CALL
PDFFilter::getSupportedServiceNames( )
290 return { "com.sun.star.document.PDFFilter" };
294 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
295 filter_PDFFilter_get_implementation(
296 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
298 return cppu::acquire(new PDFFilter(context
));
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */