Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

Xamarin.Forms iOS: How to make a PDF thumbnail?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...