Skip to content

Instantly share code, notes, and snippets.

@oxplot
Last active November 25, 2022 11:27
Show Gist options
  • Save oxplot/4b5ded9875cbba3e514f to your computer and use it in GitHub Desktop.
Save oxplot/4b5ded9875cbba3e514f to your computer and use it in GitHub Desktop.
Add hyperlinks to PDFs created by Inkscape
@thomasoliveira
Copy link

Wonderful! Thanks!
Just a suggestion on the instructions: after adding the box with the link to the SVG file, it is not enough to export it as PDF. One has also to save the SVG. Could you please explicitly add this instruction to the top of the script, please?

@orlandotrejo
Copy link

It works, thanks !

@Avibes
Copy link

Avibes commented Mar 20, 2018

Hi there!

First of all, thanks for your labor.

I cant run the code, may you can help me. I follow the spets mentioned and download the qpdf tool.

Maybe my problem is in the qpdf part. I am working with windows, and I placed the qpdf files in C:.

Well, when I try to run the script I have the following error:
image

@ALFREDOEN
Copy link

Hi! For me, it says that svglinkify.py could not open cause its damaged or not supported. How do I fix that?

@Gaibhne
Copy link

Gaibhne commented Nov 23, 2022

I wish this was still supported, the Go version is archived and Inkscape to this day can't do internal links like this script could :(

@oxplot
Copy link
Author

oxplot commented Nov 23, 2022

Just tested it on Inkscape 1.2 and it works fine.
Screencast from 2022-11-24 00-13-55.webm

@Gaibhne
Copy link

Gaibhne commented Nov 23, 2022

Wow, did not think I would see a response to something this old this fast, if ever! Sadly, you misunderstood my comment: it's internal links that don't work (such as #page=2), external links are fine. There is an open issue for that in Inkscape, but with no activity and honestly not a lot of interest, from what I can tell.

Being totally incapable of any C development, I can't help there, so I'm currently attempting to re-invent what this script must have done using a Java PDF library, as I was unable to understand the Go code, and unable to find enough information on the raw PDF syntax.

The Inkscape isse: https://gitlab.com/inkscape/inbox/-/issues/7486

@Gaibhne
Copy link

Gaibhne commented Nov 23, 2022

Addon: the Go code throws this error when attempting to use it to convert my SVG to PDF:

inkscape didn't tell us the bounding box for link '%s' - ignoring link#page=2
inkscape errored while generating PDF

@oxplot
Copy link
Author

oxplot commented Nov 23, 2022

@Gaibhne I assume you've attempted to run the svglinkify script (from the history) and that didn't work?

@Gaibhne
Copy link

Gaibhne commented Nov 23, 2022

No, until you linked it, I was unable to find the old script; the gist is now a 404 and the bit in here was changed to just the links. The Go project doesn't really have any instructions other than how to call it. Now that you've linked a version of the script, I read up on the instructions, but was unable to get it to work either; it returns with error: could not find last obj id.

I am exporting with the 'save copy as' functionality in Inkscape, as I don't know how else I would go about producing a multi page PDF. Is that correct ?

@oxplot
Copy link
Author

oxplot commented Nov 23, 2022

I can't actually see how the old script would cater for this internal linking scenario — there's no provision for it in the script. What's an example of an internally linked SVG you've used in the past that correctly jumped around the document when exported to PDF and viewed in a PDF viewer? Once we have that, we can try to make it work.

@oxplot
Copy link
Author

oxplot commented Nov 23, 2022

No, until you linked it, I was unable to find the old script;

On the top of each gist, there is a revision tab with all the changes over time. You can view the script at any point in its history.
Screenshot from 2022-11-24 01-02-56

@Gaibhne
Copy link

Gaibhne commented Nov 23, 2022

I have never managed to get that working, that is how I found my way here. I did not realize the Go version had that capability as a 'new' functionality over the old script, I guess all my searching was for vain, then :( I've tried the Go version with the instructions from the old script (no moving, #FF00FF color, etc) to no avail either. Same error as above - both for internal and regular links.

@oxplot
Copy link
Author

oxplot commented Nov 23, 2022

I'm confused.

I wish this was still supported, the Go version is archived and Inkscape to this day can't do internal links like this script could :(

When you mention "like this script could", I read that as "you tried it with svglinkify python script in the past (this gist) and you managed to create a PDF file that had working internal links". If so, then can you send me an SVG file with internal links and I'll see if I can make it work.

@oxplot
Copy link
Author

oxplot commented Nov 23, 2022

OK, I finally understand what you're referring to: the Go version of svglinkify which did explicitly support internal links:

https://github.com/oxplot/svglinkify/blob/470287cdb1b0bf2dd5632f31405d8c49b507edf6/main.go#L91

Well the Go version outputted corrupt PDFs half the time, so not really useful. It's not that hard to do a python version that uses QPDF without corrupting stuff and also supports multi page SVGs (as supported by newer Inkscape).

@Gaibhne
Copy link

Gaibhne commented Nov 23, 2022

No, I'm sorry, I'm the one that was confused - I saw the Go programs description, suggesting that it could handle internal links, and I thought the Go program to just be a rewrite of the Python script, so I assumed that was a feature of both. But with me understanding at least a little bit of Python, I wished for this script back because then I could have had a chance to reverse engineer how it was supposed to work and maybe figure out why it didn't work. In the end it seems like the Go program would have worked for me maybe with an older Inkscape version; going purely from its readme, but I have never managed to get either Go or Python version working, and only came across this whole thing today so I don't know if it would have covered my use case - a multi page PDF, with links in the PDF taking me to different pages.

What inspired me to want that was that my e-ink pad supports PDF with internal links, and you can make amazing things like what you see in the video in slide 2 here https://www.etsy.com/listing/1054473758/remarkable-2-daily-planner-standard - I wanted to make something like that for myself.

@oxplot
Copy link
Author

oxplot commented Nov 23, 2022

Gotcha — I'll give it a shot. Didn't realize that Inkscape had this feature missing.

@Gaibhne
Copy link

Gaibhne commented Nov 23, 2022

That would be super awesome, but don't go to too much trouble on my account; I'm just a hobbyist trying to make a fancy personal journal, and trying to do it the most ideal way possible :D

For what it's worth, from what I've googled today it seems that the problem is just that Inkscape prepends internal links with the file:///... stuff, and the solution might be as simple as changing the link targets in the resulting PDF, which is why I was thinking of grabbing a PDF library in a language I am fluent in and looking at opening PDFs, changing the link targets and writing them back.

@oxplot
Copy link
Author

oxplot commented Nov 24, 2022

@Gaibhne Re-wrote the script in my day off and tested it with a bunch of samples. Should be OK. Find it at https://github.com/oxplot/svglinkify

@Gaibhne
Copy link

Gaibhne commented Nov 24, 2022

That is insane, thank you so much! For me, it fails at the most basic part though - the resulting PDF has only a single page, no matter what I do. Even "New File, New Page, Save, svglinkify.py" results in only a single resulting page. I tried to exactly duplicate what you do, but nothing I did made it produce more than a single page somehow.

@Gaibhne
Copy link

Gaibhne commented Nov 24, 2022

Addendum: links are added correctly, but internal links to other pages don't work, because the objects presumably get optimized away due to not being on any page. Internal links to same-page elements produce a clickable link which work correctly (zooming in and clicking scrolls you to the target element). External links also work.

@Gaibhne
Copy link

Gaibhne commented Nov 24, 2022

Debugging shows that after # Load object IDs of all pages in the PDF document., only a single ID is found (earlier, under # Get pages and convert all measurements to pixels. all pages are correctly detected). The QDF output also shows only a single /Page, which I do not understand, especially not why it would work for you and not me. Inkscape Save Copy As correctly produces a multi-page-pdf. I'd be happy to supply files generated by me to test with but I'm not sure how to attach them here.

@oxplot
Copy link
Author

oxplot commented Nov 24, 2022

I'd be happy to supply files generated by me to test with but I'm not sure how to attach them here.

Yep, please do. You can upload your file here for e.g.: https://www.file.io/

@oxplot
Copy link
Author

oxplot commented Nov 24, 2022

Meanwhile, make sure you have your version of qpdf is not too far off of 11.1.1. Similarly, my Inkscape is at version Inkscape 1.2.1 (there seems to be a bunch of fixes for PDF export in 1.2.1 release).

@Gaibhne
Copy link

Gaibhne commented Nov 24, 2022

Oh God, I hope you didn't waste too much time on this yet. I just tested, and I did in fact have a broken Inkscape version because my upstream hadn't added the newest one yet. I got it working (although I have a file that I can't get working even after deleting every single thing in it, which I'll attach in case it's something obvious). But following your demo works perfectly for me now, so thanks!

Broken file: https://file.io/TF3IUwiLb3H1 - it's just three blank pages now, I've removed everything, but something makes the script error out with:

Traceback (most recent call last):
  File "/home/stoever/svglinkify-new/svglinkify.py", line 340, in <module>
    main()
  File "/home/stoever/svglinkify-new/svglinkify.py", line 70, in main
    svg_width_pixels = unit_2_px[svg_width[-2:]] * float(svg_width[:-2])
KeyError: '04'

@oxplot
Copy link
Author

oxplot commented Nov 24, 2022

Thanks for that. Was handling the px unit for the document size incorrectly. Just pushed a commit which works with your example file.

@Gaibhne
Copy link

Gaibhne commented Nov 24, 2022

It did, thank you again! Do you have one of those 'buy a developer a beer' kind of things ?

@oxplot
Copy link
Author

oxplot commented Nov 24, 2022

It did, thank you again! Do you have one of those 'buy a developer a beer' kind of things ?

Great. You're welcome - it was fun. I added some sponsor links to the svglinkify project page. :)

@Gaibhne
Copy link

Gaibhne commented Nov 25, 2022

I tried them all, and they are all too unconfigured to do anything :D Ko-Fi for example says "Oops, that didn't work! This creator cannot receive PayPal payments at this time.", and the others are all similar messages.

@oxplot
Copy link
Author

oxplot commented Nov 25, 2022

@Gaibhne whoops I should probably set them up 😄

EDIT: OMG, the payment part is a PITA — I guess I'm content with gratitude alone. Thanks anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment