I want to export a map to PDF with PyQGIS. I do not need to add a legend or any additional information; I just want to export the extent visible to a PDF.
If I do this manually, I just go to Project --> Import/Export --> Export Map to PDF.
All programmatic explanations involve creating a special layout and exporting from there. There's probably a very simple way to do this, but I haven't found it yet.
Code below - the export piece (borrowed from the internet and slightly modified) is the only part that doesn't work, because I honestly don't know what to put for "layoutByName()." Is there a default layout? This probably has more to do with being new to QGIS than coding, but I'm stuck.
import qgis.utils
from qgis.utils import iface
#select features
temporaryCompaction = QgsProject.instance().mapLayersByName('Temporarycompaction')[0]
iface.setActiveLayer(temporaryCompaction)
iface.mapCanvas().setSelectionColor(QColor('red'))
expression = "to_date(Date) = to_date('2021-09-08', 'yyyy-MM-dd')"
temporaryCompaction.selectByExpression(expression)
#create layer from selected features and import to Layers
fn = r'U:\skelley\public\QGISstuff\PacTrustMergin-2test/newLayer.gpkg'
writer = QgsVectorFileWriter.writeAsVectorFormat(temporaryCompaction, fn, 'utf-8', driverName='GPKG', onlySelected=True)
selected_layer = iface.addVectorLayer(fn, '', 'ogr')
del(writer)
#copy styles from parent layer
copyLayer = QgsProject.instance().mapLayersByName('Temporarycompaction')[0]
iface.setActiveLayer(copyLayer)
iface.actionCopyLayerStyle().trigger()
#paste styles to new layer
pasteLayer = QgsProject.instance().mapLayersByName('newLayer')[0]
iface.setActiveLayer(pasteLayer)
iface.actionPasteLayerStyle().trigger()
#turn off Temporarycompaction layer
toggleOff = QgsProject.instance().mapLayersByName('Temporarycompaction')[0]
iface.setActiveLayer(toggleOff)
QgsProject.instance().layerTreeRoot().findLayer(toggleOff.id()).setItemVisibilityChecked(False)
#export to pdf
dailyReport = r'U:\skelley\public\QGISstuff\PacTrustMergin-2test\tempDailyReports\dailyReport1.pdf'
projectInstance = QgsProject.instance()
layoutmanager = projectInstance.layoutManager()
layout_item = layoutmanager.layoutByName(????)
export = QgsLayoutExporter(layout_item)
export.exportToPDF(dailyReport, QgsLayoutExporter.PDFExportSettings())