# cafe-pdf

> A PDF viewer based on mozilla's PDF.js.

Latest version **3.0.1** (published 2024-01-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install cafe-pdf
pnpm add cafe-pdf
yarn add cafe-pdf
bun add cafe-pdf
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2024-01-24 |
| First published | 2021-06-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 4 MB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | wangzhe |
| Maintainers | wangdaodao |
| Keywords | pdfjs, vue |

## Links

- npm: https://www.npmjs.com/package/cafe-pdf
- npm.io page: https://npm.io/package/cafe-pdf

## Dependencies (3)

- [vue](https://npm.io/package/vue.md) 2.6.11
- [core-js](https://npm.io/package/core-js.md) ^3.6.5
- [print-js-updated](https://npm.io/package/print-js-updated.md) ^1.6.1

## Alternatives

- [@cantoo/pdf-lib](https://npm.io/package/@cantoo/pdf-lib.md) — 297.9K weekly downloads
- [datatables.net-buttons](https://npm.io/package/datatables.net-buttons.md) — 200.1K weekly downloads
- [@ckeditor/ckeditor5-export-pdf](https://npm.io/package/@ckeditor/ckeditor5-export-pdf.md) — 167.0K weekly downloads
- [scanbot-web-sdk](https://npm.io/package/scanbot-web-sdk.md) — 15.0K weekly downloads
- [@syncfusion/ej2-angular-pdfviewer](https://npm.io/package/@syncfusion/ej2-angular-pdfviewer.md) — 8.8K weekly downloads

## Recent versions

- 3.0.1 (latest) — 2024-01-24
- 3.0.0 — 2024-01-07
- 2.0.0 — 2022-06-26
- 1.3.9 — 2021-11-11
- 1.3.8 — 2021-11-08
- 1.3.7 — 2021-10-12
- 1.3.6 — 2021-10-12
- 1.3.5 — 2021-10-12
- 1.3.4 — 2021-09-09
- 1.3.3 — 2021-09-08
- 1.3.2 — 2021-09-03
- 1.3.1 — 2021-09-03
- 1.2.5 — 2021-09-01
- 1.2.4 — 2021-08-31
- 1.2.3 — 2021-07-01
- … 11 more at https://npm.io/package/cafe-pdf/versions

## README

# Cafe-PDF

![NPM Publish](https://github.com/wangdaodao/cafe-pdf/actions/workflows/npm-publish.yml/badge.svg) ![NPM version](https://img.shields.io/npm/v/cafe-pdf.svg)

> Render PDF files on Vue pages using pdf.js, supporting text rendering, lazy loading, paging, scaling, and printing.

* 😊 Vue 2.0 + [`pdfjs-dist 2.3.200`](https://github.com/mozilla/pdfjs-dist/releases/tag/v2.3.200);
* 😂 Support for modern browsers;

## Install

```shell
npm install cafe-pdf --save
```

## Quick Start
```js
import cafePdf from 'cafe-pdf'
import 'cafe-pdf/lib/index.css'
Vue.use(cafePdf)

// OR
import cafePdf from 'cafe-pdf'
import 'cafe-pdf/lib/index.css'
export default {
  // ...
  components: {
    cafePdf
  }
  // ...
}
```

## Example

Demo：[http://demo.const.team/pdf/](http://demo.const.team/pdf/)

```vue
<template>
  <cafe-pdf ref="pdf" @on-success="load" @on-scroll="scroll" :filePath="src">
    <template slot="header">
      pdf-src:<input type="text" v-model.lazy="src" @change="fileChanged">
    </template>
    <template slot="footer">
      <button @click="cw">cw</button>
      <button @click="ccw">ccw</button>
      <button :disabled="currentNum<=1" @click="pre">pre</button>
      <input type="number" :min="1" :max="pageNum" v-model.number="currentNum" @change="pageChange(currentNum)">
      <button :disabled="currentNum>=pageNum" @click="next">next</button>
      <select v-model="scale" @change="setScale">
        <option value="auto">auto</option>
        <option value="page-actual">actual</option>
        <option value="page-fit">fit</option>
        <option value="page-width">width</option>
        <option value="0.50">50%</option>
        <option value="0.75">75%</option>
        <option value="1">100%</option>
        <option value="1.25">125%</option>
        <option value="1.50">150%</option>
        <option value="1.75">175%</option>
        <option value="2">200%</option>
        <option value="3">300%</option>
        <option value="4">400%</option>
      </select>
      <button @click="print">print</button>
    </template>
  </cafe-pdf>
</template>

<script>
export default {
  name: 'app',
  data() {
    return {
      src: 'https://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf',
      pageNum: null,
      scale: 1,
      currentNum: 1
    };
  },
  components: {},
  methods: {
    fileChanged() {
      this.file = this.src;
    },
    load(val) {
      this.pageNum = val;
    },
    setScale() {
      this.$refs.pdf.changeScale(this.scale);
    },
    scroll(val) {
      this.currentNum = val;
    },
    pre(){
      this.$refs.pdf.prePage();
    },
    next(){
      this.$refs.pdf.nextPage();
    },
    pageChange(val) {
      this.$refs.pdf.goToPage(val);
    },
    print() {
      this.$refs.pdf.printFile();
    },
    cw() {
        this.$refs.pdf.rotateCW();
    },
    ccw() {
        this.$refs.pdf.rotateCCW();
    }
  }
};
</script>

<style>
html,
body {
  width: 100%;
  height: 100%;
}
* {
  margin: 0;
  padding: 0;
}
#app {
  color: #2c3e50;
  width: 100%;
  height: 100%;
}
</style>
```

## Attributes
| Attribute      | Description          | Type      | Accepted Values      | Default  |
|---------- |-------------- |---------- |--------------------------------  |-------- |
| filePath | pdf source | string | — | — |
| workerSrc | workerSrc source | string | — | https://cdn.jsdelivr.net/npm/pdfjs-dist@2.3.200/build/pdf.worker.min.js |
| cMapUrl | cMap source | string | — | https://cdn.jsdelivr.net/npm/pdfjs-dist@2.3.200/cmaps/ |
| width | pdf width | string, number | — | 100% |
| height | pdf height | string, number | — | 100% |
| useOnlyCssZoom | only use css zoom | boolean | — | false |
| showNav | show catalogue ( 
If the file does not have a directory, it does not work) | boolean | — | true |
| autoWidth | Whether to scale PDF to container width during initialization | boolean | — | false |
| textLayerMode | Render in text mode, 0 use canvas，1 use text mode | number | 0/1 | 0 |
| printLoadingText | print loading text | string | — | Loading…… |

## Events
| Event Name | Description | Parameters |
| ---- | ---- | ---- |
| on-success | success event | count page, pdf obj |
| on-error | error event | error |
| on-scroll | Triggered when scrolling pdf | Current pages , Pdf Object |
| on-progress | Loading progress | {loaded, total} |

## Methods
| Method | Description | Parameters |
| ---- | ---- | ---- |
| scale | scale | Scaling |
| prePage | pre page | — |
| nextPage | next page | — |
| rotateCW | PDF clockwise rotation | — |
| rotateCCW | PDF counterclockwise rotation | — |
| goToPage | PDF change page | page |
| printFile | Print all pdf | scale |
| destroyView | PDF destroy | — |


## Slot
| name | Description |
|------|--------|
| header | header slot |
| footer | footer slot |
| catalogue | catalogue slot |


## Constructor

This component defaults to using the **jsdelivr** CDN service. If you need to use local resources, please copy the `cmaps` and `pdf.worker.min.js` resources to the static resource directory, such as `public`.

Configure `cMapUrl='/cmaps'` in the component for cmaps. 

Configure `workerSrc='/pdf.worker.min.js'` in the component for workerSrc. 

Note the version of `@vue/cli-service`. If the version is **~4.50**, please configure `vue.config.js`:

```js
module.exports = {
  transpileDependencies: ['cafe-pdf']
}

---
_Source: https://npm.io/package/cafe-pdf · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
