QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgspdfwriter.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspdfwriter.cpp
3 -------------------------------------------------------------------
4Date : 09 October 2023
5Copyright : (C) 2023
6email : marco.hugentobler at sourcepole dot com
7
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include "qgspdfwriter.h"
18
19#include "qgsapplication.h"
20#include "qgsmaprenderertask.h"
21#include "qgsmapsettings.h"
22#include "qgsmodule.h"
23#include "qgswmsrenderer.h"
24
25namespace QgsWms
26{
27 void writeAsPdf( QgsServerInterface *serverIface, const QgsProject *project, const QgsWmsRequest &request, QgsServerResponse &response )
28 {
29 QgsWmsRenderContext context( project, serverIface );
38 context.setParameters( request.wmsParameters() );
39 QTemporaryFile tmpFile;
40 if ( !tmpFile.open() )
41 {
42 QgsDebugError( QStringLiteral( "Can't open temporary file" ) );
43 // TODO return error to the user?
44 }
45 QgsRenderer renderer( context );
46 std::unique_ptr<QgsMapRendererTask> pdfTask = renderer.getPdf( tmpFile.fileName() );
47 QgsApplication::taskManager()->addTask( pdfTask.get() );
48 pdfTask->waitForFinished();
49 response.setHeader( "Content-Type", "application/pdf" );
50 response.write( tmpFile.readAll() );
51 tmpFile.close();
52 }
53} // namespace QgsWms
static QgsTaskManager * taskManager()
Returns the application's task manager, used for managing application wide background task handling.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:109
Defines interfaces exposed by QGIS Server and made available to plugins.
Defines the response interface passed to QgsService.
virtual void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device.
virtual void setHeader(const QString &key, const QString &value)=0
Set Header entry Add Header entry to the response Note that it is usually an error to set Header afte...
long addTask(QgsTask *task, int priority=0)
Adds a task to the manager.
Map renderer for WMS requests.
std::unique_ptr< QgsMapRendererTask > getPdf(const QString &tmpFileName)
Returns a configured pdf export task.
Rendering context for the WMS renderer.
void setParameters(const QgsWmsParameters &parameters)
Sets WMS parameters.
void setFlag(Flag flag, bool on=true)
Sets or unsets a rendering flag according to the on value.
Defines request interfaces passed to WMS service.
const QgsWmsParameters & wmsParameters() const
Returns the parameters interpreted for the WMS service.
Median cut implementation.
void writeAsPdf(QgsServerInterface *serverIface, const QgsProject *project, const QgsWmsRequest &request, QgsServerResponse &response)
Output GetMap response in PDF format.
#define QgsDebugError(str)
Definition qgslogger.h:57