Solution
Using QGIS expression with function file_path()
and variable @layer
, you get the path of the current layer: file_path (@layer)
. Using this, the file you want to access must be stored in the same folder as the layer.
Concatenate this with the filename to get what you want, using function concatenate()
or even easier using pipes ||
as operator to concatenate: file_path (@layer) || '/filename.pdf'
Be sure to avoid paths containing special characters or spaces and be sure that the the filename stored in the Attribute called Fieldname
comes with the extension .pdf
at the end. Otherwise, add '.pdf'
as a string at the end of the expression:
file_path (@layer) || '/' || "Fieldname" || '.pdf'
Example
So if you have an attribute field called file
where you store the filename (e.g. my_file.pdf
), use this expression to create the raltive path to this file:
file_path (@layer) || '/' || "file"
Depending on Operation System, you might have to replace backslashes \
with forward slashes /
. To do so, use this expression (be aware that in QGIS, backslahes are double masked \\
):
replace (
file_path (@layer) || '/' || "file",
'\\',
'/'
)
Screenshot: the expression creates the path to the folder where the layer is saved and looks for a file named like the value stored in the attribute field "file"
: