You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

@pardnchiu/pdf2image

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@pardnchiu/pdf2image - npm Package Compare versions

Comparing version

to
1.1.0

README.zh.md

2

dist/pdf2image.esm.js

@@ -1,1 +0,1 @@

!function(){const e="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.min.js",t="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.worker.min.js",s="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js",n=window,a=document,r=Promise,l=Uint8Array,o=setTimeout,c=URL,d=console,p=Error,h="createElement",f="setAttribute",m="appendChild",u="script",g="link",w="head",j="body",$="href",b="dataset",y="percent",x="remove",v="file",k="width",_="height",T="catch",L="then",M="promise",P="error",U="message",D="numPages",z="length",A="type",C="replace",F="push",R="slice";(()=>{for(let e of["https://cdnjs.cloudflare.com"]){let t=a[h](g);t[f]("rel","preconnect"),t[$]=e,a[w][m](t)}for(let i of[e,t,s]){let e=a[h](g);e[f]("rel","preload"),e[f]("as",u),e[$]=i,a[w][m](e)}for(let t of[e,s]){let e=a[h](u);e.src=t,a[w][m](e)}let i=a[h](g);i[f]("rel","stylesheet"),i[$]="./dist/pdf2image.css",a[w][m](i)})();class E{constructor(){const e=a[h]("div");e.className="pdf2image-loading",e[b][y]=0;const t=a[h]("p");return t.innerText=this.#e(),e[m](t),this[j]=e,this}percent(e,t,s){const i=Math.round(e/t*100);this[j][b][y]=i,this[j].children[0].innerText=this.#e(i,s)}remove(){this[j][x]()}#e(e=0,t){return(t?"準備壓縮檔 ":"解析中 ")+e+"%"}}n.pdf2image=class{#t;#s;#i;#n;#a=[];constructor(e={}){this.#t=(e.filename||"").trim()[C](/\.pdf/,""),this.#s=e[v],this.#i={png:1,jpg:1,webp:1}[e[A]]?e[A]:"jpg",this.#n=e.scale||1.5}get images(){return this.#a}convert(){return new r(((e,s)=>{pdfjsLib.GlobalWorkerOptions.workerSrc=t,pdfjsLib.getDocument({data:new l(this.#s),cMapUrl:"//cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/cmaps/",cMapPacked:!0,useWorkerFetch:!0,verbosity:0})[M][L]((async t=>{const s=new E;a[j][m](s[j]);try{const i=[],n=t[D];let l=0;for(let e=1;e<=t[D];e++)i[F]((async()=>{const i=await t.getPage(e),r=i.getViewport({scale:this.#n}),o=a[h]("canvas");o[k]=r[k],o[_]=r[_];const c=o.getContext("2d");await i.render({canvasContext:c,viewport:r})[M];const d=o.toDataURL(`image/${this.#i}`);this.#a[F](d),l+=1,s[y](l,n)})());await r.all(i),o((t=>{e(this.#a),s[x]()}),500)}catch(e){throw s[x](),new p(`${P} Page ${i} ${e[U]}`)}}))[T]((e=>{d[P](P,e),s(`${P} ${e[U]}`)}))}))}async download(){if(null==this.#s)return;this.#a[z]<1&&await this.convert();const e=new E;return a[j][m](e[j]),new r(((t,s)=>{const i=new JSZip,n=`${this.#t[z]<1?"image":this.#t[C](/\s/g,"_")}`,r=`${this.#t[z]<1?"pdf2image":this.#t[C](/\s/g,"_")}`;for(let e=0;e<this.#a[z];e++){const t=`${n} ${e}.`+this.#i,s=this.#a[e].split(",")[1];i[v](t,s,{base64:!0})}i.generateAsync({[A]:"blob",streamFiles:!0},(t=>{e[y](t.percent,100,!0)}))[L]((s=>{const i=new Date,n=i.getFullYear(),l=("0"+(i.getMonth()+1))[R](-2),d=("0"+i.getDate())[R](-2),p=("0"+i.getHours())[R](-2),f=("0"+i.getMinutes())[R](-2),u=a[h]("a");u[$]=c.createObjectURL(s),u.download=`${r}_${n}_${l}_${d}_${p}_${f}.zip`,a[j][m](u),o((s=>{t(),u.click(),u[x](),e[x]()}),500)}))[T]((e=>{d[P](P,e),s([])}))}))}}}("undefined"==typeof window?window={}:window);export const pdf2image = window.pdf2image;
!function(){const e="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.min.js",t="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.worker.min.js",s="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js",n=window,r=document,a=Promise,o=Uint8Array,l=setTimeout,c=URL,d=console,h=Error,p="createElement",f="setAttribute",m="appendChild",u="script",g="link",w="head",y="body",j="href",b="dataset",x="percent",D="remove",v="file",M="width",$="height",k="catch",T="then",L="promise",P="error",U="message",z="numPages",A="length",C="type",F="replace",R="push",E="slice";(()=>{for(let e of["https://cdnjs.cloudflare.com"]){let t=r[p](g);t[f]("rel","preconnect"),t[j]=e,r[w][m](t)}for(let n of[e,t,s]){let e=r[p](g);e[f]("rel","preload"),e[f]("as",u),e[j]=n,r[w][m](e)}for(let t of[e,s]){let e=r[p](u);e.src=t,r[w][m](e)}let n=r[p](g);n[f]("rel","stylesheet"),n[j]="./dist/pdf2image.css",r[w][m](n)})();class O{constructor(){const e=r[p]("div");e.className="pdf2image-loading",e[b][x]=0;const t=r[p]("p");return t.innerText=this.#e(),e[m](t),this[y]=e,this}percent(e,t,s){const n=Math.round(e/t*100);this[y][b][x]=n,this[y].children[0].innerText=this.#e(n,s)}remove(){this[y][D]()}#e(e=0,t){return(t?"準備壓縮檔 ":"解析中 ")+e+"%"}}n.pdf2image=class{#t;#s;#n;#i;#r=[];constructor(e={}){this.#t=(e.filename||"").trim()[F](/\.pdf/,""),this.#s=e[v],this.#n={png:1,jpg:1,webp:1}[e[C]]?e[C]:"jpg",this.#i=e.scale||1.5}get images(){return this.#r}convert(){return new a(((e,s)=>{pdfjsLib.GlobalWorkerOptions.workerSrc=t,pdfjsLib.getDocument({data:new o(this.#s),cMapUrl:"//cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/cmaps/",cMapPacked:!0,useWorkerFetch:!0,verbosity:0})[L][T]((async t=>{const s=new O;r[y][m](s[y]);try{const n=[],i=t[z];let o=0;for(let e=1;e<=t[z];e++)n[R]((async()=>{const n=await t.getPage(e),a=n.getViewport({scale:this.#i}),l=r[p]("canvas");l[M]=a[M],l[$]=a[$];const c=l.getContext("2d");await n.render({canvasContext:c,viewport:a})[L];const d=l.toDataURL(`image/${this.#n}`);this.#r[R](d),o+=1,s[x](o,i)})());await a.all(n),l((t=>{e(this.#r),s[D]()}),500)}catch(e){throw s[D](),new h(`${P} Page ${i} ${e[U]}`)}}))[k]((e=>{d[P](P,e),s(`${P} ${e[U]}`)}))}))}#a(e){const t=new Date;return e[F](/yyyy|MM|DD|hh|mm/g,(e=>({yyyy:t.getFullYear(),MM:("0"+(t.getMonth()+1))[E](-2),DD:("0"+t.getDate())[E](-2),hh:("0"+t.getHours())[E](-2),mm:("0"+t.getMinutes())[E](-2)}[e])))}async download(){if(null==this.#s)return;this.#r[A]<1&&await this.convert();const e=new O;return r[y][m](e[y]),new a(((t,s)=>{const n=new JSZip,i=this.#a(this.#t);for(let e=0;e<this.#r[A];e++){const t=`${i} ${e}.`+this.#n,s=this.#r[e].split(",")[1];n[v](t,s,{base64:!0})}n.generateAsync({[C]:"blob",streamFiles:!0},(t=>{e[x](t.percent,100,!0)}))[T]((s=>{const n=r[p]("a");n[j]=c.createObjectURL(s),n.download=`${i}.zip`,r[y][m](n),l((s=>{t(),n.click(),n[D](),e[D]()}),500)}))[k]((e=>{d[P](P,e),s([])}))}))}}}("undefined"==typeof window?window={}:window);export const pdf2image = window.pdf2image;

@@ -1,1 +0,1 @@

!function(){const e="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.min.js",t="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.worker.min.js",s="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js",n=window,a=document,r=Promise,l=Uint8Array,o=setTimeout,c=URL,d=console,p=Error,h="createElement",f="setAttribute",m="appendChild",u="script",g="link",w="head",j="body",$="href",b="dataset",y="percent",x="remove",v="file",k="width",_="height",T="catch",L="then",M="promise",P="error",U="message",D="numPages",z="length",A="type",C="replace",F="push",R="slice";(()=>{for(let e of["https://cdnjs.cloudflare.com"]){let t=a[h](g);t[f]("rel","preconnect"),t[$]=e,a[w][m](t)}for(let i of[e,t,s]){let e=a[h](g);e[f]("rel","preload"),e[f]("as",u),e[$]=i,a[w][m](e)}for(let t of[e,s]){let e=a[h](u);e.src=t,a[w][m](e)}let i=a[h](g);i[f]("rel","stylesheet"),i[$]="./dist/pdf2image.css",a[w][m](i)})();class E{constructor(){const e=a[h]("div");e.className="pdf2image-loading",e[b][y]=0;const t=a[h]("p");return t.innerText=this.#e(),e[m](t),this[j]=e,this}percent(e,t,s){const i=Math.round(e/t*100);this[j][b][y]=i,this[j].children[0].innerText=this.#e(i,s)}remove(){this[j][x]()}#e(e=0,t){return(t?"準備壓縮檔 ":"解析中 ")+e+"%"}}n.pdf2image=class{#t;#s;#i;#n;#a=[];constructor(e={}){this.#t=(e.filename||"").trim()[C](/\.pdf/,""),this.#s=e[v],this.#i={png:1,jpg:1,webp:1}[e[A]]?e[A]:"jpg",this.#n=e.scale||1.5}get images(){return this.#a}convert(){return new r(((e,s)=>{pdfjsLib.GlobalWorkerOptions.workerSrc=t,pdfjsLib.getDocument({data:new l(this.#s),cMapUrl:"//cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/cmaps/",cMapPacked:!0,useWorkerFetch:!0,verbosity:0})[M][L]((async t=>{const s=new E;a[j][m](s[j]);try{const i=[],n=t[D];let l=0;for(let e=1;e<=t[D];e++)i[F]((async()=>{const i=await t.getPage(e),r=i.getViewport({scale:this.#n}),o=a[h]("canvas");o[k]=r[k],o[_]=r[_];const c=o.getContext("2d");await i.render({canvasContext:c,viewport:r})[M];const d=o.toDataURL(`image/${this.#i}`);this.#a[F](d),l+=1,s[y](l,n)})());await r.all(i),o((t=>{e(this.#a),s[x]()}),500)}catch(e){throw s[x](),new p(`${P} Page ${i} ${e[U]}`)}}))[T]((e=>{d[P](P,e),s(`${P} ${e[U]}`)}))}))}async download(){if(null==this.#s)return;this.#a[z]<1&&await this.convert();const e=new E;return a[j][m](e[j]),new r(((t,s)=>{const i=new JSZip,n=`${this.#t[z]<1?"image":this.#t[C](/\s/g,"_")}`,r=`${this.#t[z]<1?"pdf2image":this.#t[C](/\s/g,"_")}`;for(let e=0;e<this.#a[z];e++){const t=`${n} ${e}.`+this.#i,s=this.#a[e].split(",")[1];i[v](t,s,{base64:!0})}i.generateAsync({[A]:"blob",streamFiles:!0},(t=>{e[y](t.percent,100,!0)}))[L]((s=>{const i=new Date,n=i.getFullYear(),l=("0"+(i.getMonth()+1))[R](-2),d=("0"+i.getDate())[R](-2),p=("0"+i.getHours())[R](-2),f=("0"+i.getMinutes())[R](-2),u=a[h]("a");u[$]=c.createObjectURL(s),u.download=`${r}_${n}_${l}_${d}_${p}_${f}.zip`,a[j][m](u),o((s=>{t(),u.click(),u[x](),e[x]()}),500)}))[T]((e=>{d[P](P,e),s([])}))}))}}}("undefined"==typeof window?window={}:window);
!function(){const e="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.min.js",t="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.worker.min.js",s="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js",n=window,r=document,a=Promise,o=Uint8Array,l=setTimeout,c=URL,d=console,h=Error,p="createElement",f="setAttribute",m="appendChild",u="script",g="link",w="head",y="body",j="href",b="dataset",x="percent",D="remove",v="file",M="width",$="height",k="catch",T="then",L="promise",P="error",U="message",z="numPages",A="length",C="type",F="replace",R="push",E="slice";(()=>{for(let e of["https://cdnjs.cloudflare.com"]){let t=r[p](g);t[f]("rel","preconnect"),t[j]=e,r[w][m](t)}for(let n of[e,t,s]){let e=r[p](g);e[f]("rel","preload"),e[f]("as",u),e[j]=n,r[w][m](e)}for(let t of[e,s]){let e=r[p](u);e.src=t,r[w][m](e)}let n=r[p](g);n[f]("rel","stylesheet"),n[j]="./dist/pdf2image.css",r[w][m](n)})();class O{constructor(){const e=r[p]("div");e.className="pdf2image-loading",e[b][x]=0;const t=r[p]("p");return t.innerText=this.#e(),e[m](t),this[y]=e,this}percent(e,t,s){const n=Math.round(e/t*100);this[y][b][x]=n,this[y].children[0].innerText=this.#e(n,s)}remove(){this[y][D]()}#e(e=0,t){return(t?"準備壓縮檔 ":"解析中 ")+e+"%"}}n.pdf2image=class{#t;#s;#n;#i;#r=[];constructor(e={}){this.#t=(e.filename||"").trim()[F](/\.pdf/,""),this.#s=e[v],this.#n={png:1,jpg:1,webp:1}[e[C]]?e[C]:"jpg",this.#i=e.scale||1.5}get images(){return this.#r}convert(){return new a(((e,s)=>{pdfjsLib.GlobalWorkerOptions.workerSrc=t,pdfjsLib.getDocument({data:new o(this.#s),cMapUrl:"//cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/cmaps/",cMapPacked:!0,useWorkerFetch:!0,verbosity:0})[L][T]((async t=>{const s=new O;r[y][m](s[y]);try{const n=[],i=t[z];let o=0;for(let e=1;e<=t[z];e++)n[R]((async()=>{const n=await t.getPage(e),a=n.getViewport({scale:this.#i}),l=r[p]("canvas");l[M]=a[M],l[$]=a[$];const c=l.getContext("2d");await n.render({canvasContext:c,viewport:a})[L];const d=l.toDataURL(`image/${this.#n}`);this.#r[R](d),o+=1,s[x](o,i)})());await a.all(n),l((t=>{e(this.#r),s[D]()}),500)}catch(e){throw s[D](),new h(`${P} Page ${i} ${e[U]}`)}}))[k]((e=>{d[P](P,e),s(`${P} ${e[U]}`)}))}))}#a(e){const t=new Date;return e[F](/yyyy|MM|DD|hh|mm/g,(e=>({yyyy:t.getFullYear(),MM:("0"+(t.getMonth()+1))[E](-2),DD:("0"+t.getDate())[E](-2),hh:("0"+t.getHours())[E](-2),mm:("0"+t.getMinutes())[E](-2)}[e])))}async download(){if(null==this.#s)return;this.#r[A]<1&&await this.convert();const e=new O;return r[y][m](e[y]),new a(((t,s)=>{const n=new JSZip,i=this.#a(this.#t);for(let e=0;e<this.#r[A];e++){const t=`${i} ${e}.`+this.#n,s=this.#r[e].split(",")[1];n[v](t,s,{base64:!0})}n.generateAsync({[C]:"blob",streamFiles:!0},(t=>{e[x](t.percent,100,!0)}))[T]((s=>{const n=r[p]("a");n[j]=c.createObjectURL(s),n.download=`${i}.zip`,r[y][m](n),l((s=>{t(),n.click(),n[D](),e[D]()}),500)}))[k]((e=>{d[P](P,e),s([])}))}))}}}("undefined"==typeof window?window={}:window);
{
"name": "@pardnchiu/pdf2image",
"version": "1.0.1",
"version": "1.1.0",
"description": "pdf2image is a lightweight JavaScript library to convert PDF pages into images (PNG, JPG, WebP) with real-time progress tracking and optional ZIP compression.",

@@ -5,0 +5,0 @@ "main": "dist/pdf2image.js",

@@ -7,32 +7,29 @@ # pdf2image

[![download](https://img.shields.io/npm/dm/@pardnchiu/pdf2image)](https://www.npmjs.com/package/@pardnchiu/pdf2image)
[![jsdeliver](https://img.shields.io/jsdelivr/npm/hm/@pardnchiu/pdf2image)](https://www.jsdelivr.com/package/npm/@pardnchiu/pdf2image)
[![jsdeliver](https://img.shields.io/jsdelivr/npm/hm/@pardnchiu/pdf2image)](https://www.jsdelivr.com/package/npm/@pardnchiu/pdf2image)<br>
[![](https://img.shields.io/badge/查閱-中文版本-ffffff)](https://github.com/pardnchiu/pdf2image/blob/main/README.zh.md)
## 核心特色
## Features
### PDF 轉換為圖片
- 完美整合 [pdf.js](https://github.com/mozilla/pdf.js),實現快速解析與轉換 PDF 為圖片。
- 支援多種圖片格式:jpg、png、webp。
### PDF to Images
- Seamlessly integrates with [pdf.js](https://github.com/mozilla/pdf.js) to efficiently parse PDFs and convert them into images.
- Supports multiple image formats: `jpg`, `png`, `webp` for versatile usage.
### 壓縮下載
- 結合 [jszip.js](https://github.com/Stuk/jszip),將所有生成的圖片打包成 ZIP 檔案,一鍵下載,操作便捷。
- 自動命名檔案與壓縮檔,方便管理。
### Compressed Downloads
- Leverages [jszip.js](https://github.com/Stuk/jszip) to bundle generated images into a ZIP file for one-click download.
- Automatically names files and ZIP archives for better organization of multipage content.
### 異步處理與錯誤處理
- 使用異步設計保證流暢的用戶體驗。
- 提供詳細的錯誤資訊,便於排查問題。
### High Efficiency and Stability
- Designed with asynchronous processing to ensure a smooth user experience.
- Comprehensive error handling with detailed error messages for easier debugging.
### 異步處理與錯誤處理
- 使用異步設計保證流暢的用戶體驗。
- 提供詳細的錯誤資訊,便於排查問題。
## Installation
## 安裝方式
### 從 npm 安裝
### Install via npm
```bash
npm i @pardnchiu/nanomd
npm i @pardnchiu/pdf2image
```
### 從 CDN 引入
### Include via CDN
#### 引入 `pdf2image` 套件
#### UMD Version
```html

@@ -42,3 +39,3 @@ <script src="https://cdn.jsdelivr.net/npm/@pardnchiu/pdf2image@[VERSION]/dist/pdf2image.js"></script>

#### Module 版本
#### ES Module Version
```javascript

@@ -48,28 +45,29 @@ import { pdf2image } from "https://cdn.jsdelivr.net/npm/@pardnchiu/pdf2image@[VERSION]/dist/pdf2image.esm.js";

## 使用方法
## How to use
### 初始化 `pdf2image`
### Initialize `pdf2image`
```Javascript
const converter = new pdf2image({
// filename: "" // String
file: [file], // 透過 input 取得 file.result;
scale: 1.5, // 大小縮放,預設 1.5
type: "png" // 輸出類型 [jpg|png|webp]
filename: "image-yyyy-MM-DD_hh:mm", // Specify output filename, supports date format templates [yyyy|MM|DD|hh|mm]
file: [file], // Input PDF file from file input or other sources
scale: 1.5, // Scale factor, default is 1.5
type: "png" // Output image format [jpg|png|webp]
});
// 讀取 PDF 並轉換至圖片
// Convert PDF to images
converter.convert().then(images => {
// do something
console.log("Successfully converted images:", images);
});
// 下載 PDF
converter.download();
// Download compressed ZIP file
converter.download().then(() => {
console.log("Images have been packed and downloaded!");
});
```
## 授權條款
## License
此源代碼項目採用 [MIT](https://github.com/pardnchiu/pdf2image/blob/main/LICENSE) 許可證授權。
This source code project is licensed under the [MIT](https://github.com/pardnchiu/pdf2image/blob/main/LICENSE) License.
## 開發者
## Creator

@@ -88,2 +86,2 @@ <img src="https://avatars.githubusercontent.com/u/25631760" align="left" width="96" height="96" style="margin-right: 0.5rem;">

©️ 2024 [Pardn Chiu 邱敬幃](https://www.linkedin.com/in/pardnchiu)
©️ 2024 [邱敬幃 Pardn Chiu](https://pardn.io)