Embed a Blob using PDFObject

后端 未结 3 1150
南笙
南笙 2021-01-06 06:41

I\'m using: https://pdfobject.com/

To display an embedded pdf file on my web app. However I cannot render a pdf created from a blob

相关标签:
3条回答
  • 2021-01-06 07:28

    Try using <iframe> element, requesting resource as a Blob

    html

    <div id="my-container" class="ng-scope pdfobject-container">
        <iframe src="" type="application/pdf" width="100%" height="100%" style="overflow: auto;">
        </iframe>
    </div>
    

    javascript

    var xhr = new XMLHttpRequest();
    // load `document` from `cache`
    xhr.open("GET", "/path/to/file.pdf", true); 
    xhr.responseType = "blob";
    xhr.onload = function (e) {
        if (this.status === 200) {
            // `blob` response
            console.log(this.response);
            var file = window.URL.createObjectURL(this.response);
            document.querySelector("iframe").src = file;
    
        }
    };
    xhr.send();
    

    plnkr http://plnkr.co/edit/9E5sGfMhUeIWV9yodAUd?p=preview

    0 讨论(0)
  • 2021-01-06 07:30

    Just a complement to the answer by using iframe, we need also to use pdfjs viewer on this otherwise it will download.

    <div id="my-container" class="PDFObject-container">
        <iframe src="/pdfjs/web/viewer.html?file=blob:http://xxx:8098/fsadfsaf" type="application/pdf" width="1000" height="600" style="overflow: auto;">
        </iframe>
    </div>
    

    The blob is created from binary response creates with Javascript

    const url = window.URL.createObjectURL(new Blob([response], { type: 'application/pdf' }));
    

    I do hope PDFObject can do the work without using iframe but as tested, it's not success.

    0 讨论(0)
  • 2021-01-06 07:36

    This is what l believe you were looking for.

    function previewPdf(billName) {
    
    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'home/download?pdfBillId=' + billName, true);
    xhr.responseType = 'arraybuffer';
    xhr.onload = function(e) {
        if (this.status == 200) {
            var file = new Blob([this.response], { type: 'application/pdf' });
            var fileURL = URL.createObjectURL(file);
            var viewer = $('#viewpdf');
            PDFObject.embed(fileURL, viewer);
        }
    };
    xhr.send(); 
    }
    
    0 讨论(0)
提交回复
热议问题