DynaPDFMBS.FileLink shouldn't url encode if link is already encoded

With the following code, if filename is already URL encoded, the FIleLink method encodes the url again, which does not succeed for UTF8 characters like this:


dim fileName as String = tf.Name
fileName = EncodeURLComponent( fileName )
call pdf.FileLink(50, 50, 500, 50, fileName )

Please don’t encode it yourself.
Also better use ASCII only file names as some readers don’t use the unicode text.
And Apple Preview is sandboxed and doesn’t find the PDFs unless you opened them before.

Well… we’re running into an issue where the method does it incorrectly with that string… just try it yourself. Make tf a folderitem with the name I posted above and you’ll see that you get a file not found error because the FileLink method replaces those two characters with %3F%3F whereas the correct URL encoding is actually %E4%BD%A0%E5%A5%BD

dim as folderitem = SpecialFolder.Desktop.Child("Tar你好get.pdf")
dim fileName as String = tf.Name
call pdf.FileLink(50, 50, 500, 50, fileName )
// URL encoded into the PDF is: “file:///Users/greg/Desktop/Tar%3F%3Fget.pdf” 

Dim encoded as string = EncodeURLComponent( fileName )
// encoded is: "Tar%E4%BD%A0%E5%A5%BDget"

Could we at least have a way to pass a pre-encoded path in there?

%3F = ?

somehow 你好 get ??

Right. I think that the FileLink method is converting that filename to ascii first.

If you save the PDF with no compression (SetCompressionLevel(0)), you can see the text.
The % escaping is done by applications like Preview.app, which use the ASCII version of the file link for that.

For file encodings, it may help in some cases to make sure the file name is in decomposed unicode characters. See ConvertUnicodeToCharacterDecompositionMBS function.

just getting a chance to look at this. So in the uncompressed PDF file here’s what we’re getting for the link:


As I mentioned before, this is not coming from Apple’s Preview app. the bytes that are written into the file itself are being converted to ascii without the correct encoding sequence… even with the function that you mentioned above.

FWIW, using Xojo’s built-in PDFDocument works perfectly in that you provide a file url and it doesn’t muck with the passed value.

1 Like

You mean to use WebLink() instead of FileLink()?

I don’t think so. The thing I’m doing is pointing to a file on disk which should result in a path like:


The thing I find interesting is that your FileLink method says that it takes a path, but if I pass in the FolderItem.URLPath, the saved PDF file contains this invalid:


It looks like it’s trying to treat it as a relative path and then trying to re-encode the filename.

I’ll ask Jens about it.
As said, DynaPDF doesn’t do % encoding and we check whether it should do it for ASCII file, but in my tests that doesn’t help as Preview app doesn’t like it.

Also absolute file:// URLs probably won’t fly as you can’t know what folder names the receiver of the PDF has.

It’s wrong in the PDF file. This has nothing to do with Preview. When I do the same experiment using Xojo’s PDFDocument class, I am able to pass in the Folderitem.URLPath and everything works correctly. I’d just rather not have to retool everything if I don’t need to.

I looked on the PDFDocument example. it does file link, e.g. like this:

Dim ff As FolderItem = SpecialFolder.Desktop.Child("täst.pdf")
d.AddLinkArea(f.name,10,210, 400, tHeight*2)

that produces a web link to a file URL and that works. Same in DynaPDF goes like this:

Dim ff As FolderItem = SpecialFolder.Desktop.Child("täst.pdf")
Call pdf.Weblink(50, 50, 500, 50, ff.URLPath)

which produces the same link in the PDF:


The thing that makes trouble is FileLink, where DynaPDF writes both an unicode version and an ansi version. That one works here with Windows and Adobe Reader:

Dim ff As FolderItem = SpecialFolder.Desktop.Child("täst.pdf")
Call pdf.FileLink(50, 50, 500, 50, ff.name)

which then in Adobe Reader shows a dialog like this:

Please note that the preview in a browser won’t do such a link at all.

On macOS in preview it does not work and I get the error showing ta?st.pdf.
If I change it to pass “ta%CC%88st.pdf”, I get an error from Preview, that it % encoded the %


So I can only conclude that there is a bug in Preview, where it tries to convert the text in the PDF to an URL, which fails or gives different results in compared to Adobe Reader.

I can only suggest to stay with ASCII file names.

Open that PDF in BBEdit or something. I think what you’ll find is that it is already encoded badly in the file before it gets to Preview.

Unfortunately that’s not an option for us.