Hi,

I've found the python code to export a layout from a QGIS project directly from 
the command line which is great but the database overlays are missing from the 
resulting PDF.

The layers use the authentication manager so I suspect it is something to do 
with initialising the password manager correctly but the example I've found so 
far is for an older version. Is anyone able to point me at an example that uses 
overlays from a database?

This is what I have so far:
#!/usr/bin/env python3
import os, time
from qgis.core import (QgsProject, QgsLayoutExporter, QgsApplication, 
QgsAuthManager)

QgsApplication.setPrefixPath("/usr", True)

gui_flag = False
app = QgsApplication([], gui_flag)

app.initQgis()

project_path = os.getcwd() + '/project.qgs'

project_instance = QgsProject.instance()
project_instance.setFileName(project_path)
project_instance.read()

authMgr = QgsApplication.authManager()
if authMgr.authenticationDatabasePath():
   # already initilised => we are inside a QGIS app.
   msg = 'Inside QGIS'
else:
   # outside qgis, e.g. in a testing environment => setup env var before
   # db init
   os.environ['QGIS_AUTH_DB_DIR_PATH'] = "Path/qgis-auth.db"
   msg = 'Master password could not be set'
   assert authMgr.setMasterPassword("password", True), msg
   authMgr.init( " Path /qgis-auth.db" )

manager = QgsProject.instance().layoutManager()
timestr = time.strftime("%Y%m%d-%H%M%S")

layout = manager.layoutByName("layout ") # name of the layout
# or layout = manager.layouts()[0] # first layout

exporter = QgsLayoutExporter(layout)
exporter.exportToPdf(project_instance.absolutePath() + "/layout-" + timestr + 
".pdf",
                     QgsLayoutExporter.PdfExportSettings())

app.exitQgis()

Thanks,

Paul Wittle
[cid:image001.jpg@01D665D7.034B7860]<https://www.dorsetcouncil.gov.uk/>
Business Solutions Analyst (GIS)
ICT Operations
Dorset Council
01305 228473 
<tel:01305%20228473%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20>
dorsetcouncil.gov.uk<https://www.dorsetcouncil.gov.uk>

[cid:image002.png@01D665D7.034B7860]<https://www.facebook.com/DorsetCouncilUK>
[cid:image003.png@01D665D7.034B7860]<https://instagram.com/DorsetCouncilUK>
[cid:image004.png@01D665D7.034B7860]<https://twitter.com/DorsetCouncilUK>

This e-mail and any files transmitted with it are intended solely for the use 
of the individual or entity to whom they are addressed. It may contain 
unclassified but sensitive or protectively marked material and should be 
handled accordingly. Unless you are the named addressee (or authorised to 
receive it for the addressee) you may not copy or use it, or disclose it to 
anyone else. If you have received this transmission in error please notify the 
sender immediately. All traffic may be subject to recording and/or monitoring 
in accordance with relevant legislation. Any views expressed in this message 
are those of the individual sender, except where the sender specifies and with 
authority, states them to be the views of Dorset Council. Dorset Council does 
not accept service of documents by fax or other electronic means. Virus 
checking: Whilst all reasonable steps have been taken to ensure that this 
electronic communication and its attachments whether encoded, encrypted or 
otherwise supplied are free from computer viruses, Dorset Council accepts no 
liability in respect of any loss, cost, damage or expense suffered as a result 
of accessing this message or any of its attachments. For information on how 
Dorset Council processes your information, please see 
www.dorsetcouncil.gov.uk/416433
_______________________________________________
Qgis-user mailing list
Qgis-user@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user

Reply via email to