Skip to content

Commit

Permalink
Merge pull request #444 from mdouchin/fix-export
Browse files Browse the repository at this point in the history
Export - Fix export for QGIS > 3.28
  • Loading branch information
Gustry authored Mar 6, 2024
2 parents 986c569 + a0a4a11 commit 4834ce3
Showing 1 changed file with 61 additions and 25 deletions.
86 changes: 61 additions & 25 deletions cadastre/cadastre_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"""
import os
import random
import re
import string
import tempfile

from contextlib import contextmanager
Expand All @@ -29,8 +31,9 @@
QgsFeatureRequest,
QgsFillSymbol,
QgsLayoutExporter,
QgsLayoutFrame,
QgsLayoutGridSettings,
QgsLayoutItemLabel,
QgsLayoutItemHtml,
QgsLayoutItemMap,
QgsLayoutItemPage,
QgsLayoutMeasurement,
Expand Down Expand Up @@ -419,11 +422,23 @@ def replfunc(match):
print("%s" % msg)
return msg

@staticmethod
def random_word(length: int) -> str:
"""
Return a random string of given length
"""
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))

def createComposition(self):
"""
Create a print Layout
"""
c = QgsPrintLayout(self.mProject)
# We need to set a name to the layout, otherwise, error when exporting
# more than one time:
# RuntimeError: wrapped C/C++ object of type QgsPrintLayout has been deleted
c.setName(self.random_word(20))
c.initializeDefaults()
c.setUnits(QgsUnitTypes.LayoutMillimeters)

Expand Down Expand Up @@ -483,48 +498,65 @@ def buildComposerLabel(self, key, item, page):
"""
Add a label to the print layout for an item and page
"""
cl = QgsLayoutItemLabel(self.currentComposition)

# 1st page is a map for parcelle
dpage = page - 1
if self.etype == 'parcelle' and self.print_parcelle_page:
dpage = page

cl.attemptMove(
# create HTML layout item
# htmlItem = QgsLayoutItemHtml.create(self.currentComposition)
html_item = QgsLayoutItemHtml(self.currentComposition)

# add frame to layout
self.currentComposition.addMultiFrame(html_item)

# create frame to show content from htmlItem
html_item_frame = QgsLayoutFrame(
self.currentComposition,
html_item
)
# htmlItemFrame.attemptSetSceneRect(
# QRectF(
# item['position'][0],
# item['position'][1] + (dpage) * (self.pageHeight + 10),
# item['position'][2],
# item['position'][3]
# )
# )

# No border
html_item_frame.setFrameEnabled(False)

# set HTML contents
html_item.setContentMode(QgsLayoutItemHtml.ManualHtml)
content = self.getContentForGivenItem(
key,
item,
page
)
html_item.setHtml(content)
html_item.loadHtml()

# Reposition the frame
html_item_frame.attemptMove(
QgsLayoutPoint(
item['position'][0],
item['position'][1] + (dpage) * (self.pageHeight + 10),
QgsUnitTypes.LayoutMillimeters
)
)
cl.setFixedSize(

# Set the correct size
html_item_frame.attemptResize(
QgsLayoutSize(
item['position'][2],
item['position'][3],
QgsUnitTypes.LayoutMillimeters
)
)

cl.setVAlign(item['align'][0])
cl.setHAlign(item['align'][1])
content = self.getContentForGivenItem(
key,
item,
page
)

cl.setMargin(0)
cl.setMode(1)
cl.setText(content)
cl.setFrameEnabled(False)
if 'bgcolor' in item:
cl.setBackgroundColor(item['bgcolor'])
if 'htmlState' in item:
cl.setMode(item['htmlState'])
if 'font' in item:
cl.setFont(item['font'])

self.currentComposition.addLayoutItem(cl)
# Add frame to the HTML item
html_item.addFrame(html_item_frame)

def addParcelleMap(self):
"""
Expand Down Expand Up @@ -625,6 +657,10 @@ def exportItemAsPdf(self, comptecommunal, suffix=None) -> str:
QgsMessageLog.logMessage(f'Export PDF vers {temppath}', 'cadastre', Qgis.Info)
# print("export temppath %s" % temppath)

# add layout to layout manager: for tests only
# lmgr = self.mProject.layoutManager()
# lmgr.addLayout(self.currentComposition)

# Export as pdf
exportersettings = QgsLayoutExporter.PdfExportSettings()
exportersettings.dpi = 300
Expand Down

0 comments on commit 4834ce3

Please sign in to comment.