Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print a remote pdf #95

Closed
weihongyu12 opened this issue Nov 24, 2017 · 8 comments
Closed

print a remote pdf #95

weihongyu12 opened this issue Nov 24, 2017 · 8 comments

Comments

@weihongyu12
Copy link

Can you print a remote PDF? Existing Features Printing a remote file will report a cross-domain error, but in general, the file server may not be in the same domain as the application server

@crabbly
Copy link
Owner

crabbly commented Nov 25, 2017

@weihongyu12 , we can't print a remote pdf because of the cross-domain protection and the same-origin policy.

I'll check if we there is way to work around it. Maybe we can implement a solution in the library.

@edsonrodsilva
Copy link

I'm also looking for a way to print a remote pdf.
Using this library we have cross-origin error frame.

Can you imagine a way to do it?

@weihongyu12
Copy link
Author

Try PDF.js?

@steveklebanoff
Copy link
Contributor

You could create a server endpoint that acts a proxy.

i.e. http://myapp.com/cool_pdf.pdf could route to a server-side function that grabs the remote PDF and renders it.

@crabbly
Copy link
Owner

crabbly commented Feb 22, 2018

I haven't found a solid solution to work around the some-origin policy and allow the lib to print remove pdf files. As it uses iframe to load the pdf before printing it, the error will always occurs.

I recommend using the solution above suggested by Steve.

@crabbly crabbly closed this as completed Feb 22, 2018
@crabbly
Copy link
Owner

crabbly commented May 25, 2018

I implemented a better pdf pre-loading logic which I just realized it solved the issue when printing remote pdf files.
The file will be fetched via a http request and saved into a local blob url. When sending this url to the iframe, the browser won't block it since it is not longer a remote file.

Here is an example:
http://plnkr.co/edit/kxn1TuoP9Dxj75jrdl5W?p=preview

For now, it will only do this when preloading, which means, we have to pass the showModal parameter as true or use a hook function onLoadingStart.
I'm not sure that is worth to implement the same method by default (without preload / loading hooks).

Either way, I thought this was pretty cool and wanted to share.
Run npm update print-js to get the latest version.

@weihongyu12
Copy link
Author

Now that the remote file can be promised, I think the document should prompt the server to set Access-Control-Allow-Origin: *

@sensoeurn
Copy link

sensoeurn commented Mar 28, 2019

This worked for me. http://plnkr.co/edit/kxn1TuoP9Dxj75jrdl5W?p=preview
Vue.js(http://localhost:8083) + laravel (http://appapi.test/)
Thank you @crabbly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants