JavaScript 如何在浏览器中检查 pdf 文件是 US Legal 还是 US Letter 格式

How to check if a pdf File is in US Legal or US Letter format in the browser in JavaScript

我在浏览器中 运行。我有 File (the JavaScript File type) 类型 application/pdf。我想检查 pdf 的格式是横向还是纵向的 US Letter(8.5 英寸 x 11 英寸)或 US Legal(8.5 英寸 x 14 英寸)。

我查看了 jsPDF and though it looks great to create pdf documents programmatically (which will come in handy for tests) I was not able to find a way to use it to parse an existing PDF File 并获取了有关文档的信息(例如页面格式和方向)。

对于实现我的目标的任何帮助,无论是 jsPDF、另一个库还是 vanilla JS 都将不胜感激。

使用简单的文本解析,大多数 但不是所有 PDF 文件将有一个或多个 /MediaBox 条目,代表每一页。 /CropBox 是查看页面的大小,因此如果存在可能会更好。除非页面使用不同的 UserUnit,否则页面长度 通常 以磅为单位。格式为[x0, y0, x1, y1],因此它们可能并不总是以0开头,甚至可能是下面的值,它是x0和x1之间的差值表示标称宽度。

这里只是最近欧洲示例中的一些第一个条目,因此请注意变化,有些是整数,有些是实数,有些是混合的(就像很多 pdf 内容一样,没有强制规则)

/MediaBox [0 0 595.28 841.89]
/MediaBox [0 0 842 595]
/MediaBox[0 0 387.36 594]

对于美国字母和法律,它们通常是整数 期望 或搜索

/MediaBox[0 0 612 792] 和类似的

在许多情况下,所有页面都是相同的形状,即使稍后打算旋转,但有时页面可以混合,但这需要 search/counting 所有页面(假设所有页面都是简单的文本描述)

/MediaBox .... 0 576 720] = A US Gov Letter Portrait Page (8" x 10") [*]
/MediaBox .... 0 720 576] = A US Gov Letter Landscape Page
/MediaBox .... 0 576 756] = A US Gov Letter Portrait Page (8" x 10.5")
/MediaBox .... 0 756 576] = A US Gov Letter Landscape Page
/MediaBox .... 0 576 936] = A US Gov Legal Portrait Page (8" x 13") [*]
/MediaBox .... 0 936 576] = A US Gov Legal Landscape Page
/MediaBox .... 0 612 792] = A US Letter Portrait Page (8.5" x 11")
/MediaBox .... 0 792 612] = A US Letter Landscape Page
/MediaBox .... 0 612 936] = A US Gov Legal Portrait Page (8.5" x 13") [*]
/MediaBox .... 0 936 612] = A US Gov Legal Landscape Page
/MediaBox .... 0 612 1008] = A US Legal Portrait Page (8.5" x 14")
/MediaBox .... 0 1008 612] = A US Legal Landscape Page

还有其他具有历史意义的美国尺码

* https://en.wikipedia.org/wiki/Paper_size#Loose_sizes

旋转最常设置为 0,然后应用矩阵变换或 /action 来旋转第一个视图,在那些支持此类操作或无阻塞脚本的阅读器中。因此对于搜索 1001x /Rotate 0 个条目

没有用

因此,例如我应该补充一点,我在上面的示例中给出的第一个随机文件是一个垂直的纵向页面,又窄又高,但它是一个从右侧读取的图表,作为横向机场布局,测试将失败,因为应该以哪种方式阅读它,因为可以在 pdf 中设置纵向或横向,但是用户可以将这两个文本都阅读为纵向然后是横向