I have a web page with links to some pdf files (stored on server). The user can save web page and pdf files keeping the relation. For this I create a "special version" of the web page with hyperlinks to local files, for example:
<a href='file:///c:/myfolder/mysubfolder/linked_file.pdf'>show linked_file (absolute path)</a>
<a href='file:///mysubfolder/linked_file.pdf'>show linked_file (relative path)</a>
User saves webpage as pdf with browser print function to folder, for example c:myfoldermain_file.pdf and downloads the linked pdf files to subfolder, for example c:myfoldermysubfolderlinked_file.pdf
When opening main_file.pdf with Acrobat Reader he can open linked pdf files by clicking the link.
This works only with absolute path (first link). In macOS it is necessary to omit drive letter "c:/".
But with relative paths (second link) it doesn’t work! Using absolute path is bad because there is no chance to put the pdf files in any other location. I know you can create pdf file with relative links using Adobe Acrobat.
My question is: How do I have do specify a link with relative path in html so that links in pdf created with print function of browser (Chrome, Firefox, Safari) are working locally in Windows or macOS? Or is this impossible?
2
Answers
In general, loading web resources from mixed protocol is not possible, and that is a feature.
Further information:
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
Once published, your website will be available under HTTP(+s). So, relative URLs will work, like
./file.stuff
(since they will be loaded under the same protocol) but other protocols will be just blocked (of coursefile:///path/to/file.stuff
will be blocked in most circumstances).Having said this (that was about embedding), a web page should also never try to link local files with an absolute local pathname like
file://myfolder/mysubfolder/linked_file.pdf
since the browser web should have no info about your personal computer’s filesystem. The "myfolder" name itself, but also its pathname, are local information that web developers should try to don’t get, and don’t use.If you need to export things:
You can try to export a compressed file, containing the web page and also your PDF files. So, the user extracts everything, and you can rely on your relative structure, with predictable relative URLs like
<a href="./files/file.pdf">...</a>
etc."./mysubfolder/linked_file.pdf"
should work in many cases, but it can vary from reader to reader and device to device. Thus it is best not to use folders, simply assume same root folder.Here the HTML links to same root folder i.e. current "downloads" before save as.
If we hover before print as PDF we can see at bottom left it looks to open current working test.pdf, once printed it should do the same and look for the sibling file.
<a href="test.pdf" [other options]>samefolder test.pdf</a>