Skip to content

Instantly share code, notes, and snippets.

@oxplot
Last active November 25, 2022 11:27
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • 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
@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