<div dir="ltr"><div>Hi,</div><div><br></div><div>I have an atlas layout that uses represent_value() in several parts like labels and tables of attributes to retrieve the pretty strings in fields formatted with relation_references and value_relation widgets. Exporting the layout in atlas works fine. And using the native:atlaslayouttopdf processing algorithm directly from processing also exports correctly, with the correct labels.</div><div><br></div><div>My next step was to create an action on the coverage layer layer that runs the processing algorithm for a specific feature. The button works fine and the PDF is created, but in the labels and tables, I get the keys printed and not the represent_value() outputs. <br></div><div><br></div><div>This is the code I am using in the action (with inspiration from Ujaval Gandhi work):<br><br><span style="font-family:monospace">from qgis.core import QgsExpression, QgsExpressionContext, QgsExpressionContextUtils, QgsProject, QgsApplication, Qgis<br>from qgis.PyQt.QtCore import QUrl<br>from qgis.PyQt.QtGui import QDesktopServices<br>from qgis.utils import iface<br>from os import path<br>import processing<br>import re<br><br>uuid = '[% "uuid" %]'<br>filename = '[%concat(format_date( "data_visita",'yyyyMMdd'),'-' || represent_value("talhao_id"),'-' || represent_value("tecnico_id"),'-' || represent_value("actividade"), '.pdf')%]'<br>filename = re.sub(r'[\\/*?:"<>|]', '_', filename)<br>filename = filename.strip()<br><br><br># Create expression context to access variables<br>context = QgsExpressionContext()<br>context.appendScope(QgsExpressionContextUtils.globalScope())<br>context.appendScope(QgsExpressionContextUtils.projectScope(QgsProject.instance()))<br><br># Evaluate the expression with proper context<br>expression = QgsExpression("@working_project_path")<br>expression.prepare(context)<br>output_dir = expression.evaluate(context)<br>output_path = path.join(output_dir, 'relatorios', filename)<br><br>print(output_path)<br><br>processing_context = QgsProcessingContext()<br>processing_context.setProject(QgsProject.instance())<br>processing_context.setExpressionContext(context)<br><br>feedback = QgsProcessingFeedback()<br><br>parameters = {<br>    'COVERAGE_LAYER': 'postgres://service=\'gis\' sslmode=disable key=\'uuid\' estimatedmetadata=true checkPrimaryKeyUnicity=\'1\' table="base"."visita_monitorizacao"',<br>    'DISABLE_TILED': False,<br>    'DPI': None,<br>    'FILTER_EXPRESSION': '"uuid" = \'[% "uuid" %]\'',<br>    'FORCE_RASTER': False,<br>    'FORCE_VECTOR': False,<br>    'GEOREFERENCE': True,<br>    'IMAGE_COMPRESSION': 0,  # Lossy (JPEG)<br>    'INCLUDE_METADATA': True,<br>    'LAYERS': None,<br>    'LAYOUT': 'Atlas visitas',<br>    'OUTPUT': output_path,<br>    'SIMPLIFY': True,<br>    'SORTBY_EXPRESSION': '',<br>    'SORTBY_REVERSE': False,<br>    'TEXT_FORMAT': 0,  # Always Export Text as Paths (Recommended)<br>}<br><br>processing.run(<br>    'native:atlaslayouttopdf',<br>    parameters,<br>    context=processing_context,<br>    feedback=feedback<br>    )<br><br># Show message in QGIS toolbar with link<br>file_url = QUrl.fromLocalFile(output_path)<br>message = f'Relatório exportado com sucesso para <a href="{file_url.toString()}">{file_url.toString()}</a>'<br><br>item = iface.messageBar().pushMessage(<br>    "Exportação:",<br>    message,<br>    level=Qgis.Info,<br>    duration=0  # Stays until user closes it<br>)</span><br><br></div><div>I thought the problem could be the algorithm running without a proper context, and that is why I added <span style="font-family:monospace">processing_context</span><span style="font-family:arial,sans-serif"> lines, but it actually made no difference.</span></div><div><br></div><div>The workaround is replacing the </div><div><br></div><div><span style="font-family:monospace">represent_values("field_with_key") </span></div><div><br></div><div>by </div><div><br></div><div><span style="font-family:monospace">attributes(get_feature('layer', 'id', "field_with_key")["value_field"]</span></div><div><span style="font-family:monospace"><br></span></div><div>in the layout for all labels and tables, but now I would really learn what I am doing wrong :-)</div><div><br></div><div>Thanks,</div><div><br></div><div>Alex Neto</div></div>