【问题标题】:how can I display my file name using PDF.js?如何使用 PDF.js 显示我的文件名?
【发布时间】:2018-09-14 22:33:13
【问题描述】:

我正在尝试使用 pdf.js 显示 PDF 文档。

我还可以显示页数、当前页等。但不知道如何显示文件名!这是我的代码:

<div id="pdf-title"></div>
<div id="page-count-container">Page
    <div id="pdf-current-page"></div> of <div id="pdf-total-pages">
    </div>
</div>
</div>
..
<script>
    ..
    var title;
    var __PDF_DOC,
        __CURRENT_PAGE,
        __TOTAL_PAGES,
        __PAGE_RENDERING_IN_PROGRESS = 0,
        __CANVAS = $('#pdf-canvas').get(0),
        __CANVAS_CTX = __CANVAS.getContext('2d');


    function showPDF(pdf_url) {
        $("#pdf-loader").show();

        PDFJS.getDocument({
            url: pdf_url
        }).then(function(pdf_doc) {
            __PDF_DOC = pdf_doc;
            __TOTAL_PAGES = __PDF_DOC.numPages;
            title = __PDF_DOC._filename;

            // Hide the pdf loader and show pdf container in HTML
            $("#pdf-loader").hide();
            $("#pdf-contents").show();
            $("#pdf-total-pages").text(__TOTAL_PAGES);
            $("#pdf-title").text(toString(title));

            // Show the first page
            showPage(1);
        }).catch(function(error) {
            // If error re-show the upload button
            $("#pdf-loader").hide();
            $("#upload-button").show();

            alert(error.message);
        });;
    }..
</script>

谁能帮帮我?

【问题讨论】:

    标签: javascript pdf.js


    【解决方案1】:

    我想出了一种方法来解决这个不便,这是我的代码:

    var __PDF_DOC,
        __CURRENT_PAGE,
        __TOTAL_PAGES,
        __PAGE_RENDERING_IN_PROGRESS = 0,
        __CANVAS = $('#pdf-canvas').get(0),
        __TITLE;
        __CANVAS_CTX = __CANVAS.getContext('2d');
    
    
    function showPDF(pdf_url) {
        $("#pdf-loader").show();
    
        PDFJS.getDocument({ url: pdf_url }).then(function(pdf_doc) {
            __PDF_DOC = pdf_doc;
            __TOTAL_PAGES = __PDF_DOC.numPages;
            __TITLE=$("#file-to-upload").get(0).files[0].name;
    
            // Hide the pdf loader and show pdf container in HTML
            $("#pdf-loader").hide();
            $("#pdf-contents").show();
            $("#pdf-total-pages").text(__TOTAL_PAGES);
            $("#pdf-title").text(__TITLE);
    

    我实际上是在调用错误的变量!希望这会对你们中的一些人有所帮助!

    【讨论】:

      猜你喜欢
      • 2018-01-23
      • 1970-01-01
      • 2014-08-18
      • 2015-09-27
      • 2022-01-17
      • 2019-11-06
      • 2014-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多