4

I am trying to link a PDF for a shapefile, but I'm unable to set a relative path. How can I set a relative path in QGIS? I am only able to open the PDF in my PC, whereas when share this file to others, they are able to open the file only when they change the path in the attributes. Note: The attcachment and shapefile are in two different subfolders. The path for shapefile is "F:\GIS\SHP\POZZETTI_.shx" whereas path for PDF is "F:\GIS\PDF\PDF_POZZETTI\POZZETTO_1.pdf"

enter image description here

enter image description here

enter image description here

enter image description here

2
  • 1
    Welcome to GIS SE. As a new user, please take the Tour. "I tried a lot of things" isn't very descriptive. Your goal here is to write not only a question that can be answered, but a Question that can be found by others with the same problem.
    – Vince
    Apr 1, 2023 at 12:15
  • A shapefile is a set of three files with the same 8.3 prefix ending in .shp, .shx, and .dbf (plus other optional files with the same prefix). The correct way to reference it is by the .shp.
    – Vince
    Apr 4, 2023 at 11:59

1 Answer 1

4

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": enter image description here

15
  • Hello , Thank you your response, but it's not working for me , so i will give my project details. So that could you please write the exact condition which will be helpful for me. My Qgis project folder is D:\learn\qgis and the shapfile layer name is trail . Whereas my pdf is the same folder of the project as 1.pdf . So could you please write me the condition in my case that could set me a relative path. Thank you Apr 1, 2023 at 18:58
  • It depends where exactly you want to have the path and for what? You want to create an attribute value as a text string?
    – Babel
    Apr 1, 2023 at 19:03
  • Yes i created an attribute value as text and assigned action to it. So when send this project to my friend in his computer the pdf is not opening. Because of the path difference. Apr 1, 2023 at 20:58
  • See updated answer.
    – Babel
    Apr 2, 2023 at 14:26
  • F:\Akwa Dropbox\Lavori\02-F\2022-5 Consulenza Frana Petralia Sottana\2023_FOGNATURA_PETRALIA\GIS\PDF\PDF_POZZETTI\POZZETTO_1.pdf only wirth this path i am able to open PDF on my computer not others. I tried the above expression given by babel , but i dont know what the problem is it is not working. Apr 3, 2023 at 8:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.