1

I'm embedding PDF files to a very simple HTML page and everything works fine, but I need a fallback in case the source file does not exist. If the src file is not found an alternative file should be displayed (not-found.pdf for instance).

The problem is very similar as the one on the topic below but with PDF files.

Inputting a default image in case the src attribute of an html <img> is not valid?

I've tried the solutions provided on that topic but none of them worked.

I'm using a simple embed tag:

<embed src="../files/ID 41279.pdf" width="1026px" height="1780" onerror="if (this.src != '../files/not-found.pdf') this.src = '../files/not-found.pdf';">

As you can see I tried to use javascript (as suggested on the topic above) but nothing happens. On the console the error 404 (for the original file) show as usual but the document is blank.

Is it possible to do that with PDF files? It only need to work on Chrome, so compatibility with other browsers is not necessary.

A pure HTML solution would be better but using javascript or jquery is ok too.

Also, the fallback doesn't need to be a PDF file, I can make do with an image or event text (as long as I can format it).

Thanks!

Community
  • 1
  • 1
Lucas Kunzler
  • 109
  • 2
  • 9
  • 1
    I cannot spot any query in that code... But anyway: "does not work" is not very helpful. What does that mean? Nothing happens at all? You get an error message? The universe implodes? I suggest you open your browsers development console and check if a javascript error is raised. – arkascha Aug 01 '15 at 09:17
  • 1
    Nothing happens, the document returns blank. Meant javascript, sorry, – Lucas Kunzler Aug 01 '15 at 09:19
  • "returns"? why that? I thought you want to embed the document? – arkascha Aug 01 '15 at 09:20
  • The Chrome pdf viewer is completely blank if the src file is not found, the console shows the 404 error. – Lucas Kunzler Aug 01 '15 at 09:23
  • OK. And what URL does the console show that is answered by the 404? Probably the URL does not point to an existing document. So what should be returned? – arkascha Aug 01 '15 at 09:23
  • The src URL. http://localhost/project/files/ID%2041279.pdf Failed to load resource: the server responded with a status of 404 (Not Found)" The console doesn't mention the not-found.pdf file. – Lucas Kunzler Aug 01 '15 at 09:26
  • OK, so that inline js code does not work. So back to my question above: any javascript errors in the console? – arkascha Aug 01 '15 at 09:26
  • Nope, the only error on the console is the 404. – Lucas Kunzler Aug 01 '15 at 09:28
  • 1
    not sure whether the error event will fire here in the first place, have you tried with an alert()? – Pekka Aug 01 '15 at 09:32
  • You are right, I tried an alert() and it didn't show up. Any ideas? – Lucas Kunzler Aug 01 '15 at 09:35

1 Answers1

1

Although this is not an orthodox answer to your question, you can handle the error with apache configuration (if you are using apache server) redirecting on missing file error as mentioned here Redirect requests only if the file is not found? but maybe you can achieve similar results with other servers.

Community
  • 1
  • 1
Mario
  • 420
  • 1
  • 11
  • 23
  • It's definitely doable but wouldn't be practical on my case, this webpage will run on several offline machines and editing each apache server would be a pain. :( Thanks for the answer anyway. – Lucas Kunzler Aug 01 '15 at 10:04