如何使用 pdf.js 确定 pdf 的大小,以便缩放到屏幕尺寸?

问题描述 投票:0回答:2

我需要缩放pdf,使其填充屏幕的高度,但我找到的只是函数

scale()
。我怎么知道要缩放多少?

javascript html pdf pdf.js
2个回答
12
投票

要确定页面的宽度/高度,您需要执行以下操作:

  1. 获取页面

  2. 在“promise”函数中(只能异步检索页面):

    a.检索比例为

    1

    的视口

    b.调用视口的

    width
    属性

代码:

//Get the page with your callback
pdf.getPage(1).then( function(page) {

    //We need to pass it a scale for "getViewport" to work
    var scale = 1;

    //Grab the viewport with original scale
    var viewport = page.getViewport( 1 );

    //Here's the width and height
    console.log( "Width: " + viewport.width + ", Height: " + viewport.height );
});

0
投票

从 getViewport 获取 pdf 宽度和高度(以英寸为单位)

let viewport= page.getViewport({ scale: 1 });
let inchHeight=viewport.height/72;
let inchWidth=viewport.width/72;

转换为像素

 let pixleHeight=inchHeight*96;
 let pixleWidth=inchWidth*96;

从 Adobe 中查找文档页面尺寸(英寸) -> 在 Adobe 中打开文档,转到文件 -> 属性或 ctrl+d

© www.soinside.com 2019 - 2024. All rights reserved.