2

This code

<embed src="http://www.star.le.ac.uk/~slc25/curry.pdf" type="application/pdf" />

<iframe src="http://docs.google.com/gview?url=https://d9db56472fd41226d193-1e5e0d4b7948acaf6080b0dce0b35ed5.ssl.cf1.rackcdn.com/spectools/docs/wd-spectools-word-sample-04.doc&embedded=true"></iframe>

embeds a PDF document and an MS Word document into an HTML web page.

However, for MS Word, it is reliant on Google, which has a habit of offering neat stuff and then withdrawing it, just as people become reliant on it; so I would like something which I have control over - a JS library from my server.

I have found ViewerJs, which is some excellent FOSS - just check out the demos.

Alas, it says "Because of the way your browser protects itself against security risks (which is a very good thing) ViewerJS only works on documents that reside on your own server".

I understand that the are trying to prevent Cross Site Scripting attacks, but I have a requirement to embed documents from a different site (which is also under my control).

Does anyone know of a gratis JS library which will allow me to embed MS Word & PDF documents into a web page when those documents come from a different domain from the web page?


[Update] Maybe I just explained it badly.

Site A hosts the docs, which should be embedded in a page on site B.

I control site A and I also provide the code for the single page of site B which should display the embedded document.

Site B's owner will load that page to their site once, after I have coded it, but will make no other changes to site B. The end user gets a URL by email, with a link to the page on site B with some parameters to indicate what should be embedded


[Update++] This Plunk does it, but uses Google Docs, and I would rather not rely on any code from a 3rd party site (I don't mind 3rd party code; I just want it on my site)

No one? Not even for a bonus? I would be happy to award more points for a good answer. Lots more points

2

Using another library solves nothing, this is a browser security feature.

You can make the first site download a copy of the file before displaying it.

But if you have control over the external server you can configure cross-origin access to your files. Read about the CORS specification.

In Apache do it by enable the mod_headers module then, in the .htaccess or virtual host config

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "http://site2.com"
</IfModule>

or:

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>
  • Google docs manages it. I just want something that does the same, but over the code of which I have control, not Google. I have control of the server which hosts the DOC/PDF files, but not the server which serves the web pages. – Mawg Sep 27 '17 at 19:29
  • If you don't have such a control over the external server/site you cannot do this any other way than making the first site download a copy of it before displaying, no matter of what display library you use – Sauxo Sep 28 '17 at 11:14
  • Am I I missing something? The code that I give above embeds 2 randomly Googled files, one PDF and one MS Word, into my web page. There appear to be no cross-site problems. However, the MS Word is embedded using some code on Google's web site & I would rather use code over which I have control. Please see update to question, with more explanation. – Mawg Sep 28 '17 at 13:36
  • [This Plunk](achieves it). Alas, I can't seem to get it work on my site ... yet :-( – Mawg Oct 2 '17 at 11:54
  • 1
    @Mawg: Was your last comment supposed to contain a link? – Nicolas Raoul Oct 5 '17 at 5:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.