I'm developing an Angular application that needs to display PDF files. To achieve this, I'm currenty using the [ng2-pdf-viewer][1] component. The component works fine for files like:
I do however run into CORS-issues with files like:
The error message I receive:
Failed to load htt://frontendmasters.com/assets/resources/lukasruebbelke/better-apps-angular-2-day1.pdf: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'htt://localhost:4200' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
core.es5.js:1020 ERROR Error: Uncaught (in promise): DataCloneError: Failed to execute 'postMessage' on 'Worker': TypeError: Failed to fetch could not be cloned. Error: Failed to execute 'postMessage' on 'Worker': TypeError: Failed to fetch could not be cloned.
My Code
component.html
<pdf-viewer
[src]="document.url.url"
[page]="1"
[original-size]="false"
style="display: block;">
</pdf-viewer>
component.ts
document = {
name: 'Angular 2',
description: 'An amazing Angular 2 pdf',
url: {
url: 'https://frontendmasters.com/assets/resources/lukasruebbelke/better-apps-angular-2-day1.pdf',
withCredentials: true
}
}
If you want me to provide any additional information, please let me know.
For those who work with AWS S3, this issu could be solved by configuration of CORS.
<CORSConfiguration>
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
for more infomation https://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With