0

I tried to make the "add html" item in the layout display a paragraph of text.This code works well in Python console:

project = QgsProject.instance()
layout_manager = project.layoutManager()
template_layout = layout_manager.layoutByName('test')
layout = template_layout.clone()
item = layout.itemById('html')
if isinstance(item , QgsLayoutFrame):
 multi_frame = item.multiFrame() 
 if isinstance(multi_frame, QgsLayoutItemHtml):
 html = multi_frame
 
item.setFrameEnabled(False)
html.setContentMode(QgsLayoutItemHtml.ManualHtml)
html.setHtml('this is a test!')
html.loadHtml() 
settings = QgsLayoutExporter.ImageExportSettings()
settings.dpi = 300 
output_path = r"D:\a.jpg"
exporter = QgsLayoutExporter(layout)
exporter.exportToImage(output_path, settings) 
layout_manager.removeLayout(layout)

But the same code cannot work properly in the Processing ToolBox:

def processAlgorithm(self,parameters: dict[str, Any],context: QgsProcessingContext,feedback: QgsProcessingFeedback,) -> dict[str, Any]:
 ...
 project = context.project()
 layout_manager = project.layoutManager()
 template_layout = layout_manager.layoutByName('test')
 layout = template_layout.clone()
 item = layout.itemById('html')
 if isinstance(item , QgsLayoutFrame):
 multi_frame = item.multiFrame() 
 if isinstance(multi_frame, QgsLayoutItemHtml):
 html = multi_frame
 
 item.setFrameEnabled(False)
 html.setContentMode(QgsLayoutItemHtml.ManualHtml)
 html.setHtml('this is a test!')
 html.loadHtml() 
 settings = QgsLayoutExporter.ImageExportSettings()
 settings.dpi = 300 
 output_path = r"D:\b.jpg"
 exporter = QgsLayoutExporter(layout)
 exporter.exportToImage(output_path, settings) 
 layout_manager.removeLayout(layout)
 ...

What happened and how should I modify it?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 26 at 13:05
4
  • gis.stackexchange.com/questions/493906/… Commented Jun 26 at 13:22
  • 1
    thank you!thank you very much! Commented Jun 26 at 13:37
  • 1
    Please provide the exact QGIS version used and explain what do you mean exactly with <<cannot work properly>>. Commented Jun 26 at 14:11
  • 1
    html item display nothing when the code run in the Processing ToolBox. thanks Ben W,i get "processing algorithms are run in a background thread by default and any non-thread-safe API calls made in the processAlgorithm() method will result in crashes & unexpected behavior". Commented Jun 28 at 11:51

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.