[QGIS-Developer] Issues with represent_value used inside native:atlaslayouttopdf processing algorithm

Alexandre Neto senhor.neto at gmail.com
Fri Nov 14 11:24:44 PST 2025


Hi,

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.

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.

This is the code I am using in the action (with inspiration from Ujaval
Gandhi work):

from qgis.core import QgsExpression, QgsExpressionContext,
QgsExpressionContextUtils, QgsProject, QgsApplication, Qgis
from qgis.PyQt.QtCore import QUrl
from qgis.PyQt.QtGui import QDesktopServices
from qgis.utils import iface
from os import path
import processing
import re

uuid = '[% "uuid" %]'
filename = '[%concat(format_date( "data_visita",'yyyyMMdd'),'-' ||
represent_value("talhao_id"),'-' || represent_value("tecnico_id"),'-' ||
represent_value("actividade"), '.pdf')%]'
filename = re.sub(r'[\\/*?:"<>|]', '_', filename)
filename = filename.strip()


# Create expression context to access variables
context = QgsExpressionContext()
context.appendScope(QgsExpressionContextUtils.globalScope())
context.appendScope(QgsExpressionContextUtils.projectScope(QgsProject.instance()))

# Evaluate the expression with proper context
expression = QgsExpression("@working_project_path")
expression.prepare(context)
output_dir = expression.evaluate(context)
output_path = path.join(output_dir, 'relatorios', filename)

print(output_path)

processing_context = QgsProcessingContext()
processing_context.setProject(QgsProject.instance())
processing_context.setExpressionContext(context)

feedback = QgsProcessingFeedback()

parameters = {
    'COVERAGE_LAYER': 'postgres://service=\'gis\' sslmode=disable
key=\'uuid\' estimatedmetadata=true checkPrimaryKeyUnicity=\'1\'
table="base"."visita_monitorizacao"',
    'DISABLE_TILED': False,
    'DPI': None,
    'FILTER_EXPRESSION': '"uuid" = \'[% "uuid" %]\'',
    'FORCE_RASTER': False,
    'FORCE_VECTOR': False,
    'GEOREFERENCE': True,
    'IMAGE_COMPRESSION': 0,  # Lossy (JPEG)
    'INCLUDE_METADATA': True,
    'LAYERS': None,
    'LAYOUT': 'Atlas visitas',
    'OUTPUT': output_path,
    'SIMPLIFY': True,
    'SORTBY_EXPRESSION': '',
    'SORTBY_REVERSE': False,
    'TEXT_FORMAT': 0,  # Always Export Text as Paths (Recommended)
}

processing.run(
    'native:atlaslayouttopdf',
    parameters,
    context=processing_context,
    feedback=feedback
    )

# Show message in QGIS toolbar with link
file_url = QUrl.fromLocalFile(output_path)
message = f'Relatório exportado com sucesso para <a
href="{file_url.toString()}">{file_url.toString()}</a>'

item = iface.messageBar().pushMessage(
    "Exportação:",
    message,
    level=Qgis.Info,
    duration=0  # Stays until user closes it
)

I thought the problem could be the algorithm running without a proper
context, and that is why I added processing_context lines, but it actually
made no difference.

The workaround is replacing the

represent_values("field_with_key")

by

attributes(get_feature('layer', 'id', "field_with_key")["value_field"]

in the layout for all labels and tables, but now I would really learn what
I am doing wrong :-)

Thanks,

Alex Neto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20251114/a7608ddc/attachment.htm>


More information about the QGIS-Developer mailing list