# pdfViewer **Repository Path**: belltan/pdfViewer ## Basic Information - **Project Name**: pdfViewer - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 18 - **Created**: 2024-05-28 - **Last Updated**: 2024-05-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # pdfViewer ## Introduction pdfViewer is a PDF reader in JavaScript. Its [pdfjs](https://gitee.com/openharmony-tpc/pdfViewer/blob/master/pdfJs/README.md) module allows you to display PDF documents on OpenHarmony devices, and its [library](https://gitee.com/openharmony-tpc/pdfViewer/blob/master/library/README.md) module lets you add JPG and PNG images to PDF documents. This project is modified based on the open-source libraries [pdf.js](https://github.com/mozilla/pdf.js) and [jspdf](https://github.com/parallax/jsPDF) to adapt to the OpenHarmony component project. ## Installing Modules Run the following **npm install** command: ``` npm install @ohos/pdfjs ``` For details, see [Installing an OpenHarmony npm Package](https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_npm_usage.md). Run the following **ohpm install** command: ``` ohpm install @ohos/img2pdf ``` For details, see [Installing an OpenHarmony ohpm Package](https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_har_usage.md). ## How to Use ### Configure the global resourceManager. For pdfjs to obtain a resource file, you need to configure the global resourceManager in the **EntryAbility** file. ``` import { GlobalContext } from '../pages/globalThis' GlobalContext.getContext().setObject("context", this.context) ``` ### 1. Use the **getDocument(src:Uint8Array)** method to load the PDF document to be opened. ``` import { GlobalContext } from '../pages/globalThis' gloContext: Context = GlobalContext.getContext().getObject("context") as Context gloContext.resourceManager.getRawFile(pdfFilePath, (error: Error, value: Uint8Array) => { if (error != null) { console.log(error.message); } else { this.getPDFDocument(value); } }) ``` ### 2. Obtain pages in the target PDF document through **loadingTask**. ``` loadingTask.promise.then((pdf) => { // Obtain the first page in the PDF document. pdf.getPage(1).then((page) => { // TODO: Perform the getViewport operation. }) }) ``` ### 3. Use **getViewport()** to obtain the viewport size for the PDF document, with the display ratio specified. ``` page.getViewport({ scale: 1 }).width page.getViewport({ scale: 1 }).height ``` ### 4. Add an image to a PDF document. The following is a pseudocode example. You need to specify **imageData** and **path** on your own. ``` import jsPDF from "@ohos/img2pdf" let imageData = imageData let data = path let jsPdf = new jsPDF(); jsPdf.addImage(imageData, 50, 50, 150, 150); let content = jsPdf.buildDocument(); let buf = jsPdf.getArrayBuffer(content) const writer = fileio.openSync(data + `/pdfMessage.pdf`, 0o102, 0o666); fileio.writeSync(writer, buf); ``` ### 5. Add multiple images to a PDF document. The following is a pseudocode example. You need to specify **imageData**, **imageData1**, **imageData2**, and **path** on your own. ``` import jsPDF from "@ohos/img2pdf" let imageData = imageData let imageData1 = imageData1 let imageData2 = imageData2 let data = path let jsPdf = new jsPDF(); jsPdf.addImage(imageData, 10, 10, 20, 20); jsPdf.addImage(imageData1, 70, 10, 100, 40); jsPdf.addImage(imageData2, 100, 100, 100, 40); let content = jsPdf.buildDocument(); let buf = jsPdf.getArrayBuffer(content) const writer = fileio.openSync(data + `/pdfMessage.pdf`, 0o102, 0o666); fileio.writeSync(writer, buf); ``` For more methods for adding images to a PDF document, see [jsPDF](https://artskydj.github.io/jsPDF/docs/module-addImage.html), by which the OpenHarmony component img2pdf is powered. ## Available APIs ### 1. Reading a PDF Document | API | Input Parameter | Description | | :------------------------: | :---------------------: | :---------------------------------------: | | getDocument | src | Obtains a PDF document. It is the main entry for loading and interacting with a PDF document. | | setPDFNetworkStreamFactory | pdfNetworkStreamFactory | Returns a callback that resolves to an { IPDFStream } instance. | | annotationStorage | | Stores annotation data in a form. | | getPageLayout | | Returns a promise containing a string of page layout names.| ### 2. Adding Images to a PDF Document | API | Input Parameter | Description | | :------------: | :----------------------------------------------------------: | :---------------------------------: | | jsPDF | **options**: settings initializing the jsPDF instance. By default, the format is A4, the orientation is landscape , and the unit is millimeter. | Initializes the jsPDF instance. | | addImage | **imageData**: image content
**arguments**: image size | Adds an image to a PDF document. | | buildDocument | | Obtains the content data of the target PDF document. | | getArrayBuffer | **data**: string to be converted | Returns an **ArrayBuffer** object. | | addPage | **format**: page format
**orientation**: page direction | Adds a page to (and moves focus to) a PDF document.| | movePage | **targetPage**: number of the page after being moved
**beforePage**: number of the page before being moved | Moves two PDF pages. | | setPage | **page**: target number of the page. | Sets the position of a page.| | insertPage | **beforePage**: number of the page to be inserted. | Inserts a page. | | deletePage | **targetPage**: number of the page to delete. | Deletes a page. | | text | **text**: text to add
**x**: horizontal coordinate of the text
**y**: vertical coordinate of the text
**options**: format settings of the text| Adds text to a PDF page. | | output | **type**: output type
**options**: output settings | Generates the PDF document. | | getPageWidth | | Obtains the width of the current page. | | getPageHeight | | Obtains the height of the current page. | ## Directory Structure ``` /pdfViewer # Project code |—— entry # Project demo |—— library │ └── src │ └── main │ └── ets │ └── components │ └── btoa │ └── fflate │ └── libs │ └── modules │ └── jspdf.js │ └── index.ets # External APIs ├── pdfJs │ └── src │ └── main │ └── ets │ └── components │ └── core │ └── display │ └── shared │ └── dca-library.js │ └── deepCopyAll.js │ └── index.ets # External APIs ``` ## Constraints pdfViewer has been verified in the following versions: DevEco Studio: 4.0 Release(4.0.3.513), SDK: API10 (4.0.10.10) DevEco Studio: 4.0 Beta2(4.0.3.512), SDK: API10 (4.0.10.9) DevEco Studio: 4.0Canary2(4.0.3.312), SDK: API10(4.0.9.2) ## License This project is based on [Apache License 2.0](https://gitee.com/openharmony-tpc/pdfViewer/blob/master/LICENSE). ## How to Contribute If you find any problem when using pdfViewer, submit an [issue](https://gitee.com/openharmony-tpc/pdfViewer/issues) or a [PR](https://gitee.com/openharmony-tpc/pdfViewer/pulls) to us.