async-pdf
Version:
Create, Modify and Merge PDF Files
288 lines (287 loc) • 7.98 kB
TypeScript
import { PDFFont } from 'pdf-lib';
import { PDFFontTypes } from '../types';
import { PDFCreateOptions, PDFLineOptions, PDFPageSpacing, PDFRectangleOptions, PDFTextOptions } from '../interfaces';
export declare class PDF {
private document;
private file;
private unit;
private fontSize;
private tmpDir;
private orientation;
private pageSize;
private page;
private pageFraming;
private pageSpacing;
private fontName;
private font;
private fontColor;
private mergeFiles;
private pagesControl;
private limits;
private pageSelected;
private externalFontPath;
/**
* Create a new [[PDF]].
* @returns Resolves with the newly created document.
*/
static create(options?: PDFCreateOptions): Promise<PDF>;
private constructor();
/**
* Set page spacing.
*
*/
setPageSpacing(pageSpacing: PDFPageSpacing): void;
/**
* Set the page font.
* That doesn't chage the general font.
*/
setPageFont(font: PDFFont): void;
/**
* Select page.
*/
selectPage(pageNumber: number): Promise<void>;
/**
* Clear the page.
*/
clearPage(): Promise<void>;
/**
* Remove the page by page number.
* Example:
* ```js
* PDF.removePage(1) // Remove the first page of the document
* PDF.removePage(2) // Remove the second page of the document
* PDF.removePage(200) // Remove the 200th page of the document
* ```
*/
removePage(pageNumber: number): Promise<void>;
/**
* Add page to the document.
* Example:
* ```js
*let pageOptions = {
* unit: 'mm',
* orientation: 'landscape',
* pageSize: { line: 297, column: 210 },
* pageSpacing: { top: 10, bottom: 10, left: 10, right: 10 },
* font: 'Helvetica-Bold',
*}
* PDF.addPage(pageOptions) // The document will have two pages. The second page will have the new options
* PDF.addPage() // The document will have two pages. The second page will have the same first page options
* ```
*/
addPage(pageOptions?: PDFCreateOptions): Promise<void>;
/**
* Write a text on the page.
* Example:
* ```js
* // It will write a text aligned by right direction
* PDF.writeText(`Hello world`, {
* align: 'right',
* size: 20,
* position: {
* linePosition: 210,
* columnPosition: 200,
* }
* })
* ```
*/
writeText(text: string, options: PDFTextOptions): Promise<void>;
/**
* Write a line on the page.
* Example:
* ```js
* // It will write a vertical red line at midle of the page with 50% of opacity
* PDF.writeLine({
* start: {
* linePosition: 100,
* columnPosition: 0,
* },
* end: {
* linePosition: 100,
* columnPosition: 297,
* },
* color: { r: 1, g: 0, b: 0, a: 0.5 },
* thickness: 1
* })
* ```
*/
writeLine(options: PDFLineOptions): void;
/**
* Write a rectangular on the page.
* Example:
* ```js
* // It will write a rectangular starting at line 10 and column 50 with size of 100 width, 50 height and gray background color
* let options: PDFRectangleOptions = {
start: { linePosition: 10, columnPosition: 50 },
area: { width: 100, height: 500 },
areaColor: { r: 0.95, g: 0.95, b: 0.95, a: 1 },
}
* PDF.writeRectangle(options)
* ```
*/
writeRectangle(options: PDFRectangleOptions): void;
/**
* Get a text size.
* Example:
* @returns Resolve with a text height.
* ```js
* PDF.getHeighAtSize(20,'Helvetica-Bold')
* ```
*/
getHeighAtSize(size: number, font: PDFFontTypes | PDFFont): Promise<number>;
/**
* Get a text size.
* Example:
* @returns Resolve with a text width.
* ```js
* PDF.getWidthOfTextAtSize('test',20,'Helvetica-Bold')
* ```
*/
getWidthOfTextAtSize(text: string, size: number, font?: PDFFontTypes | PDFFont): Promise<number>;
getWidthOfTextAtSizeByPageFont(text: string, size: number): number;
/**
* Get a [[PDFFont]] by external font.
* Example:
* @returns Resolve with a PDFFont.
* ```js
* let font = await PDF.getCustomFont("../fonts/HouschkaHead-BoldItalic.otf")
* })
*```
*/
private getCustomFont;
/**
* Get a [[PDFFont]] by name.
* Example:
* @returns Resolve with a PDFFont.
* ```js
* let font = await PDF.getCustomFont("Helvetica")
* })
*```
*/
getFontByName(fontTypes: PDFFontTypes): Promise<PDFFont>;
/**
* Get a text size.
* Example:
* ```js
* PDF.setCustomFont("../fonts/HouschkaHead-BoldItalic.otf")
* ```
*/
setCustomFont(fontPath: string): Promise<void>;
private getFont;
/**
* Save the document at the file path
* For example:
* ```js
* PDF.save("/out/pdf/test.pdf")
* ```
*/
save(filePath: string): Promise<void>;
/**
* Load an external pdf
* For example:
* ```js
* // test.pdf has 5 pages
* PDF.getNumberOfPages() // returns 1
* PDF.loadPDF("/out/pdf/test.pdf")
* PDF.addPage();
* PDF.getNumberOfPages() // returns 7
* ```
*/
loadPDF(pdfFilePath: string): Promise<void>;
/**
* Merge multiples pdf file to the document
* For example:
* ```js
* // test.pdf has 3 pages and test2.pdf has 2 pages.
* PDF.getNumberOfPages() // returns 1
* PDF.removePage(1);
* PDF.getNumberOfPages() // returns 0;
* let pdfFilesPath = ["/out/pdf/test.pdf","/out/pdf/test2.pdf"]
* PDF.mergePDF(pdfFilesPath)
* PDF.getNumberOfPages() // returns 5
* ```
*/
mergePDF(pdfFilesPath: string[]): Promise<void>;
/**
* Get the number of pages.
* Example:
* @returns - Number of pages.
* ```js
* PDF.addPage()
* PDF.getNumberOfPages() // 2
* ```
*/
getNumberOfPages(): number;
/**
* Get page width.
* Example:
* @returns - Returns page width.
* ```js
* let pageOptions = {
* unit: 'mm',
* orientation: 'portrait',
* pageSize: { line: 210, column: 297 },
* pageSpacing: { top: 10, bottom: 10, left: 10, right: 10 },
* font: 'Helvetica-Bold',
*}
* PDF.addPage(pageOptions)
* PDF.getPageWidth() // 210
* ```
*/
getPageWidth(): number;
/**
* Get page height.
* Example:
* @returns - Returns page height.
* ```js
* let pageOptions = {
* unit: 'mm',
* orientation: 'portrait',
* pageSize: { line: 210, column: 297 },
* pageSpacing: { top: 10, bottom: 10, left: 10, right: 10 },
* font: 'Helvetica-Bold',
*}
* PDF.getPageHeight() // 297
* ```
*/
getPageHeight(): number;
/**
* Get the document font.
* Example:
* @returns - Returns a [[PDFFont]].
* ```js
* PDF.getDocumentFont()
* ```
*/
getDocumentFont(): PDFFont;
private createDocument;
private createPage;
private getColorRGBFromRGBA;
private getAlfaFromRGBA;
private normalizeText;
private normalizeLine;
private normalizePositions;
private normalizePageSpacing;
private columnNormalize;
private verifyPositionsByLimit;
private verifyColumnByLimit;
private verifyLineByLimit;
private setPageLimits;
private wasLastPageSaved;
private saveTheLastPage;
private savePage;
private deletePageFile;
private getPageFile;
private mergeGroupOfPDF;
private setOrientation;
private isNegative;
private pageDoesNotExist;
private pagesForMergeDoesNotExist;
private columnIsOutRange;
private columnWithHeightIsOutRange;
private lineIsOutRange;
private lineWithWidthIsOutRange;
private negativeNumber;
private filePathDoesNotExist;
private pdfFilesPathEmpty;
}