LibreOffice Module sdext (master) 1
pdfiadaptor.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
21#include "pdfiadaptor.hxx"
22#include "filterdet.hxx"
23#include <saxemitter.hxx>
24#include <odfemitter.hxx>
25#include "inc/wrapper.hxx"
26#include <pdfiprocessor.hxx>
27
28#include <osl/file.h>
29#include <sal/log.hxx>
30#include <rtl/ref.hxx>
31
33#include <com/sun/star/lang/IllegalArgumentException.hpp>
34#include <com/sun/star/lang/XMultiComponentFactory.hpp>
35#include <com/sun/star/io/XSeekable.hpp>
37
38#include <memory>
39
40using namespace com::sun::star;
41
42
43namespace pdfi
44{
45
46PDFIHybridAdaptor::PDFIHybridAdaptor( const uno::Reference< uno::XComponentContext >& xContext ) :
47 m_xContext( xContext )
48{
49}
50
51// XFilter
52sal_Bool SAL_CALL PDFIHybridAdaptor::filter( const uno::Sequence< beans::PropertyValue >& rFilterData )
53{
54 bool bRet = false;
55 if( m_xModel.is() )
56 {
57 uno::Reference< io::XStream > xSubStream;
58 OUString aPwd;
59 const beans::PropertyValue* pAttribs = rFilterData.getConstArray();
60 sal_Int32 nAttribs = rFilterData.getLength();
61 sal_Int32 nPwPos = -1;
62 for( sal_Int32 i = 0; i < nAttribs; i++ )
63 {
64 SAL_INFO("sdext.pdfimport", "filter: Attrib: " << pAttribs[i].Name
65 << " = " << (pAttribs[i].Value.has<OUString>()
66 ? pAttribs[i].Value.get<OUString>()
67 : OUString("<no string>"))
68 << "\n");
69 if ( pAttribs[i].Name == "EmbeddedSubstream" )
70 pAttribs[i].Value >>= xSubStream;
71 else if ( pAttribs[i].Name == "Password" )
72 {
73 nPwPos = i;
74 pAttribs[i].Value >>= aPwd;
75 }
76 }
77 bool bAddPwdProp = false;
78 if( ! xSubStream.is() )
79 {
80 uno::Reference< io::XInputStream > xInput;
81 auto pAttr = std::find_if(rFilterData.begin(), rFilterData.end(),
82 [](const beans::PropertyValue& rAttr) { return rAttr.Name == "InputStream"; });
83 if (pAttr != rFilterData.end())
84 pAttr->Value >>= xInput;
85 if( xInput.is() )
86 {
87 // TODO(P2): extracting hybrid substream twice - once during detection, second time here
88 uno::Reference< io::XSeekable > xSeek( xInput, uno::UNO_QUERY );
89 if( xSeek.is() )
90 xSeek->seek( 0 );
91 oslFileHandle aFile = nullptr;
92 sal_uInt64 nWritten = 0;
93 OUString aURL;
94 if( osl_createTempFile( nullptr, &aFile, &aURL.pData ) == osl_File_E_None )
95 {
96 SAL_INFO("sdext.pdfimport", "created temp file " << aURL);
97 const sal_Int32 nBufSize = 4096;
98 uno::Sequence<sal_Int8> aBuf(nBufSize);
99 // copy the bytes
100 sal_Int32 nBytes;
101 do
102 {
103 nBytes = xInput->readBytes( aBuf, nBufSize );
104 if( nBytes > 0 )
105 {
106 osl_writeFile( aFile, aBuf.getConstArray(), nBytes, &nWritten );
107 if( static_cast<sal_Int32>(nWritten) != nBytes )
108 {
109 xInput.clear();
110 break;
111 }
112 }
113 } while( nBytes == nBufSize );
114 osl_closeFile( aFile );
115 if( xInput.is() )
116 {
117 OUString aEmbedMimetype;
118 OUString aOrgPwd( aPwd );
119 xSubStream = getAdditionalStream( aURL, aEmbedMimetype, aPwd, m_xContext, rFilterData, true );
120 if( aOrgPwd != aPwd )
121 bAddPwdProp = true;
122 }
123 osl_removeFile( aURL.pData );
124 }
125 else
126 xSubStream.clear();
127 }
128 }
129 if( xSubStream.is() )
130 {
131 uno::Sequence< uno::Any > aArgs{ uno::Any(m_xModel), uno::Any(xSubStream) };
132
133 SAL_INFO("sdext.pdfimport", "try to instantiate subfilter" );
134 uno::Reference< document::XFilter > xSubFilter;
135 try {
136 xSubFilter.set(
137 m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
138 "com.sun.star.document.OwnSubFilter",
139 aArgs,
140 m_xContext ),
141 uno::UNO_QUERY );
142 }
143 catch(const uno::Exception&)
144 {
145 TOOLS_INFO_EXCEPTION("sdext.pdfimport", "subfilter");
146 }
147
148 SAL_INFO("sdext.pdfimport", "subfilter: " << xSubFilter.get() );
149 if( xSubFilter.is() )
150 {
151 if( bAddPwdProp )
152 {
153 uno::Sequence<beans::PropertyValue> aFilterData( rFilterData );
154 if( nPwPos == -1 )
155 {
156 nPwPos = aFilterData.getLength();
157 aFilterData.realloc( nPwPos+1 );
158 aFilterData.getArray()[nPwPos].Name = "Password";
159 }
160 aFilterData.getArray()[nPwPos].Value <<= aPwd;
161 bRet = xSubFilter->filter( aFilterData );
162 }
163 else
164 bRet = xSubFilter->filter( rFilterData );
165 }
166 }
167 else
168 SAL_INFO("sdext.pdfimport", "PDFIAdaptor::filter: no embedded substream set" );
169 }
170 else
171 SAL_INFO("sdext.pdfimport", "PDFIAdaptor::filter: no model set" );
172
173 return bRet;
174}
175
177{
178}
179
180//XImporter
181void SAL_CALL PDFIHybridAdaptor::setTargetDocument( const uno::Reference< lang::XComponent >& xDocument )
182{
183 SAL_INFO("sdext.pdfimport", "PDFIAdaptor::setTargetDocument" );
184 m_xModel.set( xDocument, uno::UNO_QUERY );
185 if( xDocument.is() && ! m_xModel.is() )
186 throw lang::IllegalArgumentException();
187}
188
190{
191 return "org.libreoffice.comp.documents.HybridPDFImport";
192}
193
194sal_Bool PDFIHybridAdaptor::supportsService(OUString const & ServiceName)
195{
197}
198
200{
201 return {"com.sun.star.document.ImportFilter"};
202}
203
204PDFIRawAdaptor::PDFIRawAdaptor( OUString const & implementationName, const uno::Reference< uno::XComponentContext >& xContext ) :
205 m_implementationName(implementationName),
206 m_xContext( xContext )
207{
208}
209
211{
212 m_pVisitorFactory = rVisitorFactory;
213}
214
215bool PDFIRawAdaptor::parse( const uno::Reference<io::XInputStream>& xInput,
216 const uno::Reference<task::XInteractionHandler>& xIHdl,
217 const OUString& rPwd,
218 const uno::Reference<task::XStatusIndicator>& xStatus,
219 const XmlEmitterSharedPtr& rEmitter,
220 const OUString& rURL,
221 const OUString& rFilterOptions )
222{
223 // container for metaformat
224 auto pSink = std::make_shared<PDFIProcessor>(xStatus, m_xContext);
225
226 bool bSuccess=false;
227
228 if( xInput.is() )
229 bSuccess = xpdf_ImportFromStream( xInput, pSink, xIHdl,
230 rPwd, m_xContext, rFilterOptions );
231 else
232 bSuccess = xpdf_ImportFromFile( rURL, pSink, xIHdl,
233 rPwd, m_xContext, rFilterOptions );
234
235 if( bSuccess )
236 pSink->emit(*rEmitter,*m_pVisitorFactory);
237
238 return bSuccess;
239}
240
241bool PDFIRawAdaptor::odfConvert( const OUString& rURL,
242 const uno::Reference<io::XOutputStream>& xOutput,
243 const uno::Reference<task::XStatusIndicator>& xStatus )
244{
245 XmlEmitterSharedPtr pEmitter = createOdfEmitter(xOutput);
246 const bool bSuccess = parse(uno::Reference<io::XInputStream>(),
247 uno::Reference<task::XInteractionHandler>(),
248 OUString(),
249 xStatus,pEmitter,rURL, "");
250
251 // tell input stream that it is no longer needed
252 xOutput->closeOutput();
253
254 return bSuccess;
255}
256
257// XImportFilter
258sal_Bool SAL_CALL PDFIRawAdaptor::importer( const uno::Sequence< beans::PropertyValue >& rSourceData,
259 const uno::Reference< xml::sax::XDocumentHandler >& rHdl,
260 const uno::Sequence< OUString >& /*rUserData*/ )
261{
262 // get the InputStream carrying the PDF content
263 uno::Reference< io::XInputStream > xInput;
264 uno::Reference< task::XStatusIndicator > xStatus;
265 uno::Reference< task::XInteractionHandler > xInteractionHandler;
266 OUString aURL;
267 OUString aPwd;
268 OUString aFilterOptions;
269 for( const beans::PropertyValue& rAttrib : rSourceData )
270 {
271 SAL_INFO("sdext.pdfimport", "importer Attrib: " << rAttrib.Name );
272 if ( rAttrib.Name == "InputStream" )
273 rAttrib.Value >>= xInput;
274 else if ( rAttrib.Name == "URL" )
275 rAttrib.Value >>= aURL;
276 else if ( rAttrib.Name == "StatusIndicator" )
277 rAttrib.Value >>= xStatus;
278 else if ( rAttrib.Name == "InteractionHandler" )
279 rAttrib.Value >>= xInteractionHandler;
280 else if ( rAttrib.Name == "Password" )
281 rAttrib.Value >>= aPwd;
282 else if ( rAttrib.Name == "FilterOptions" )
283 rAttrib.Value >>= aFilterOptions;
284 }
285 if( !xInput.is() )
286 return false;
287
288 XmlEmitterSharedPtr pEmitter = createSaxEmitter(rHdl);
289 const bool bSuccess = parse(xInput, xInteractionHandler,
290 aPwd, xStatus, pEmitter, aURL, aFilterOptions);
291
292 // tell input stream that it is no longer needed
293 xInput->closeInput();
294 xInput.clear();
295
296 return bSuccess;
297}
298
299//XImporter
300void SAL_CALL PDFIRawAdaptor::setTargetDocument( const uno::Reference< lang::XComponent >& xDocument )
301{
302 SAL_INFO("sdext.pdfimport", "PDFIAdaptor::setTargetDocument" );
303 m_xModel.set( xDocument, uno::UNO_QUERY );
304 if( xDocument.is() && ! m_xModel.is() )
305 throw lang::IllegalArgumentException();
306}
307
309{
311}
312
313sal_Bool PDFIRawAdaptor::supportsService(OUString const & ServiceName)
314{
316}
317
318css::uno::Sequence<OUString> PDFIRawAdaptor::getSupportedServiceNames()
319{
320 return {"com.sun.star.document.ImportFilter"};
321}
322
323
324
325extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
327 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
328{
329 rtl::Reference<pdfi::PDFIRawAdaptor> pAdaptor = new pdfi::PDFIRawAdaptor( "org.libreoffice.comp.documents.WriterPDFImport", context );
330 pAdaptor->setTreeVisitorFactory(pdfi::createWriterTreeVisitorFactory());
331 pAdaptor->acquire();
332 return getXWeak(pAdaptor.get());
333}
334
335extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
337 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
338{
339 rtl::Reference<pdfi::PDFIRawAdaptor> pAdaptor = new pdfi::PDFIRawAdaptor( "org.libreoffice.comp.documents.DrawPDFImport", context );
340 pAdaptor->setTreeVisitorFactory(pdfi::createDrawTreeVisitorFactory());
341 pAdaptor->acquire();
342 return getXWeak(pAdaptor.get());
343}
344
345extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
347 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
348{
349 rtl::Reference<pdfi::PDFIRawAdaptor> pAdaptor = new pdfi::PDFIRawAdaptor( "org.libreoffice.comp.documents.ImpressPDFImport", context );
350 pAdaptor->setTreeVisitorFactory(pdfi::createImpressTreeVisitorFactory());
351 pAdaptor->acquire();
352 return getXWeak(pAdaptor.get());
353}
354
355extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
357 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
358{
359 return cppu::acquire(new pdfi::PDFIHybridAdaptor( context ));
360}
361
362}
363
364/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SAL_CALL cancel() override
sal_Bool SAL_CALL supportsService(OUString const &ServiceName) override
css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual sal_Bool SAL_CALL filter(const css::uno::Sequence< css::beans::PropertyValue > &rFilterData) override
Definition: pdfiadaptor.cxx:52
virtual void SAL_CALL setTargetDocument(const css::uno::Reference< css::lang::XComponent > &xDocument) override
OUString SAL_CALL getImplementationName() override
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: pdfiadaptor.hxx:51
PDFIHybridAdaptor(const css::uno::Reference< css::uno::XComponentContext > &xContext)
Definition: pdfiadaptor.cxx:46
css::uno::Reference< css::frame::XModel > m_xModel
Definition: pdfiadaptor.hxx:53
Adapts raw pdf import to XImportFilter interface.
Definition: pdfiadaptor.hxx:81
bool parse(const css::uno::Reference< css::io::XInputStream > &xInput, const css::uno::Reference< css::task::XInteractionHandler > &xIHdl, const OUString &rPwd, const css::uno::Reference< css::task::XStatusIndicator > &xStatus, const XmlEmitterSharedPtr &rEmitter, const OUString &rURL, const OUString &rFilterOptions)
bool odfConvert(const OUString &rURL, const css::uno::Reference< css::io::XOutputStream > &xOutput, const css::uno::Reference< css::task::XStatusIndicator > &xStatus)
Export pdf document to ODG.
css::uno::Reference< css::frame::XModel > m_xModel
Definition: pdfiadaptor.hxx:87
virtual void SAL_CALL setTargetDocument(const css::uno::Reference< css::lang::XComponent > &xDocument) override
TreeVisitorFactorySharedPtr m_pVisitorFactory
Definition: pdfiadaptor.hxx:88
css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
void setTreeVisitorFactory(const TreeVisitorFactorySharedPtr &rVisitorFactory)
Set factory object used to create the tree visitors.
PDFIRawAdaptor(OUString const &implementationName, const css::uno::Reference< css::uno::XComponentContext > &xContext)
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: pdfiadaptor.hxx:85
OUString SAL_CALL getImplementationName() override
sal_Bool SAL_CALL supportsService(OUString const &ServiceName) override
OUString const m_implementationName
Definition: pdfiadaptor.hxx:83
virtual sal_Bool SAL_CALL importer(const css::uno::Sequence< css::beans::PropertyValue > &rSourceData, const css::uno::Reference< css::xml::sax::XDocumentHandler > &rHdl, const css::uno::Sequence< OUString > &rUserData) override
#define TOOLS_INFO_EXCEPTION(area, stream)
URL aURL
#define SAL_INFO(area, stream)
aBuf
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Value
int i
constexpr OUStringLiteral implementationName
XmlEmitterSharedPtr createSaxEmitter(const css::uno::Reference< css::xml::sax::XDocumentHandler > &xDocHdl)
bool xpdf_ImportFromStream(const css::uno::Reference< css::io::XInputStream > &xInput, const ContentSinkSharedPtr &rSink, const css::uno::Reference< css::task::XInteractionHandler > &xIHdl, const OUString &rPwd, const css::uno::Reference< css::uno::XComponentContext > &xContext, const OUString &rFilterOptions)
bool xpdf_ImportFromFile(const OUString &rURL, const ContentSinkSharedPtr &rSink, const css::uno::Reference< css::task::XInteractionHandler > &xIHdl, const OUString &rPwd, const css::uno::Reference< css::uno::XComponentContext > &xContext, const OUString &rFilterOptions)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * sdext_PDFIRawAdaptor_Writer_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
uno::Reference< io::XStream > getAdditionalStream(const OUString &rInPDFFileURL, OUString &rOutMimetype, OUString &io_rPwd, const uno::Reference< uno::XComponentContext > &xContext, const uno::Sequence< beans::PropertyValue > &rFilterData, bool bMayUseUI)
Definition: filterdet.cxx:515
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * sdext_PDFIRawAdaptor_Draw_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
XmlEmitterSharedPtr createOdfEmitter(const css::uno::Reference< css::io::XOutputStream > &xOut)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * sdext_PDFIRawAdaptor_Impress_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
std::shared_ptr< TreeVisitorFactory > TreeVisitorFactorySharedPtr
TreeVisitorFactorySharedPtr createWriterTreeVisitorFactory()
TreeVisitorFactorySharedPtr createDrawTreeVisitorFactory()
std::shared_ptr< XmlEmitter > XmlEmitterSharedPtr
Definition: xmlemitter.hxx:48
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * sdext_PDFIHybridAdaptor_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
TreeVisitorFactorySharedPtr createImpressTreeVisitorFactory()
OUString Name
unsigned char sal_Bool
const uno::Reference< uno::XComponentContext > m_xContext
Definition: wrapper.cxx:144