My app has a Documents folder with a number of PDF files, which have to be represented in a listview by thumbnails. I use pdf.js to generate a first page image inside a WebView.
This approach works fine for Android, but not on iOS. WkWebView does not load the PDF file. It throws an error inside my pdfPreView() function saying it cannot access the PDF.
This is the Content of the WebView:
public class PdfThumbnail : WebView
{
...
var _thumb = "file:////var/mobile/Containers/Data/Application/5DE26435-E018-4E31-B9CF-C76A79F2C11C/Documents/db/demo/upload/ba_415_de_gb.pdf";
var htmlSource = new HtmlWebViewSource();
htmlSource.BaseUrl = NSBundle.MainBundle.BundlePath; // for iOS
htmlSource.Html = $@"
<html>
<script src='pdfjs/build/pdf.js'></script>
<script src='js/PdfPreview.js'></script>
<body style='background-color:white;'>
<div style='display:flex; justify-content:center'>
<canvas id='preview'></canvas>
</div>
<script type='text/javascript'>pdfPreView('{_thumb}','preview')</script>
</body>
</html>
";
Here the JS pdfPreView function:
function pdfPreView(url, canvasId, width=0, height=0) {
var pdfjsLib = window['pdfjs-dist/build/pdf'];
pdfjsLib.GlobalWorkerOptions.workerSrc = 'pdfjs/build/pdf.worker.js'
// Asynchronous download of PDF
var loadingTask = pdfjsLib.getDocument(url);
loadingTask.promise.then(function (pdf) {
console.log('PDF loaded');
// Fetch the first page
...
}, function (reason) {
// PDF loading error
console.error(reason);
});
}
I assume WkWebView does not allow access to a file based URL pointing to the Documents folder. But how to enable this?
question from:
https://stackoverflow.com/questions/65853456/xamarin-forms-ios-how-to-make-a-pdf-thumbnail 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…