I am developing an application using PDFTron PDF Viewer library. It allows you to load a PDF in various ways:
https://www.pdftron.com/documentation/web/guides/basics/open/
My ASP.NET controller can most easily return to the view a Byte[] with the PDF contents. My issue is how to do I best use this Byte[] to load the PDFTron Viewer (javascript) in the view? The only way I have managed to do this so far involves converting the Byte[] to a Base64 string and then returning that to view. The view then uses JS code to convert the Base64 string to a Blob using the a function the PDFTron docs suggested:
function base64ToBlob(base64) {
const binaryString = window.atob(base64);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; ++i) {
bytes[i] = binaryString.charCodeAt(i);
}
return new Blob([bytes], { type: 'application/pdf' });
};
Then that Blob can be loaded into the PDFTron viewer. However, this seems to be quite convoluted and slow. Is there a more direct way the PDFTron Viewer could utilise the C# Byte[]? The PDFTron Viewer will accept a Blob or ArrayBuffer but I wasn't sure how best to convert the Byte[] the view receives to either of these types.