1

I´m looking for a solution to connect a specific PDF file to each polygon in my shape and to create an action to open the PDF. Relative paths are required.

I am aware many people already asked this kind of question, but they´ve been asked and answered years ago.

I´m using QGIS Madeira 3.4.11 (long term release) and Windows 10 (64 bit).

My file structure is like this:

Gemeinde.qgs

-Daten

--Oekokonto

---Datenblaetter

----10.pdf

With absolute paths I did it like this:

action type: open

action: [%Datenblatt%]

action

The absolute filepath is in the column "Datenblatt" in my attribute table.

Example: X:/Projekte/QGIS/Daten/Oekokonto/Datenblaetter/10.pdf

What do I have to change to have relative instead of absolute paths?

Edit:

Attribute table:

attribute table

action_2

  • 1
    Are the PDFs stored in the same directory as your shapefiles? Is there a column which just has the name of the pdf (e.g. "10.pdf")? – Joseph Aug 28 at 10:29
  • The Shapefile is saved in "Daten". Yes a column with the number exists. (e.g. 10.pdf should be linked to 10 in the mentioned column). – Barbara. Aug 28 at 11:10
1

The following is a Python method you could try. Change your Typ to Python and then use some code like the following:

import subprocess
path = QgsProject.instance().readPath("./") + "/Daten/Oekokonto/Datenblaetter/"
file = path + "[%Maßnahm_Nr%]" + ".pdf"
subprocess.Popen([file],shell=True)

Action to load pdf

  • 1
    It works. Thank you! :) – Barbara. Aug 28 at 13:26
  • 1
    @Barbara. - Most welcome, glad it helped :). The method by nielsgerrits can also be useful when you want to update the paths in your attributes. – Joseph Aug 28 at 13:29
2

You could use @project_path (this is where the QGIS project file lives) to replace the deprecated part with the project folder.

For example, if you have

D:\tmp\New folder (38)\data\test.txt

then use

[%replace(path,'D:\tmp\New folder (38)',@project_path)%]
  • I used [%replace(path,'X:/Projekte/',@project_path)%] as action. But when I click with it on a polygon nothing happens. – Barbara. Aug 28 at 11:44
  • If your projectfile is in X:/Projekte/QGIS, you need to use [%replace(path,'X:/Projekte/QGIS',@project_path)%]. If you hit the sum sign you can see the preview in the lower left corner and check if the replace string is producing what you need. – nielsgerrits Aug 28 at 11:47
  • You´re right. I changed it, but there is still nothing happening. Not even an error message. – Barbara. Aug 28 at 11:51
  • May need to change the backslashes with slashes . [%replace(replace(path,'X:/Projekte/QGIS',@project_path),'\\','/')%] (Unix vs Windows.) – nielsgerrits Aug 28 at 11:56
  • Do I need to replace them in the attribute table as well and if that is the case replace them with \ or \\ ? – Barbara. Aug 28 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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