Forum Xamarin.Forms

UWP displaying local pdf in pdf.js

JDGJDG USUniversity

All,

I had a question that I was hoping someone has run across or solved...

I am displaying local pdf's in a xamarin forms app using pdf.js.

I used the following recipe verbatim..
https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/controls/display-pdf/

It is basically working. No problem with ios, no problem with android, and for a while, no problem with UWP.

I recently set up another development machine and moved the project over to it.

Everything rebuilt fine, iOS version ran fine. Android version ran fine. But am having problems with the UWP version with regards to pdf.js.

When I try to load a local PDF I get the following error in PDF.js

PDF.js v1.5.188 (build: 0e2d50f)
Message: Unexpected server response (0) while retrieving PDF "ms-appx-web:///Assets/content/(xyz.pdf)".

I chased a number of leads on this and ultimately felt like it has to do with cross origin requests. So, to test it, I changed some of the javascript code in pdf.js.

I changed the following code snippet:
// Checks if URLs have the same origin. For non-HTTP based URLs, returns false.
function isSameOrigin(baseUrl, otherUrl) {
try {
var base = new URL(baseUrl);
if (!base.origin || base.origin === 'null') {
return false; // non-HTTP url
}
} catch (e) {
return false;
}

var other = new URL(otherUrl, base);
return base.origin === other.origin;
}

I forced the return to always be true. The result of this is that the PDF now displays correctly.

So I think I proved it has to do with the whole cross origin stuff.

My questions are:
1. Why would this work on one windows development machine and not another? Can you set windows (browsers) to somehow allow cross origin and therefore it works on one machine but not the other?
2. Does the xamarin recipe for displaying local pdf's not work anymore because pdf.js has changed?
3. Is forcing the cross site thing to true in pdf.js a terrible idea?

Answers

  • omarantonio16omarantonio16 Member ✭✭

    Se puede mostrar el pdf en uwp guardandolo en la carpeta Assets, y en el renderizador del webview le pasas solamente el nombre del archivo con la extension.
    Pero si vas a subir la aplicacion a la tienda no funciona, porque la carpeta Assets es solo de lectura.

Sign In or Register to comment.