[hummus-recipe] Implement PDF Metadata Type Definition (#40390)

* [hummus-recipe] Implement Metadata Type Definition

* Added tests
This commit is contained in:
Sebastian Martinez
2019-11-19 11:42:25 -08:00
committed by Sheetal Nandi
parent fdaf58a532
commit 9f5be9a404
2 changed files with 81 additions and 102 deletions
+18 -12
View File
@@ -1,38 +1,44 @@
import Recipe = require("hummus-recipe");
import Recipe = require('hummus-recipe');
import fs = require("fs");
import fs = require('fs');
// $ExpectType Recipe
const newDoc = new Recipe("new", "test.pdf", {
const newDoc = new Recipe('new', 'test.pdf', {
version: 1.6,
author: "John Doe",
title: "Hummus Recipe",
subject: "A brand new PDF"
author: 'John Doe',
title: 'Hummus Recipe',
subject: 'A brand new PDF',
});
// $ExpectType Recipe
newDoc
.createPage(595, 842)
.text("Memento Mori", 100, 100)
.text('Memento Mori', 100, 100)
.endPage()
.endPDF();
// $ExpectError
newDoc.createPage("A5")
.text("Memento Mori", 100, 100)
newDoc.createPage('A5')
.text('Memento Mori', 100, 100)
.endPage()
.endPDF();
const inBuffer: Buffer = fs.readFileSync("test.pdf");
const inBuffer: Buffer = fs.readFileSync('test.pdf');
const bufferDoc = new Recipe(inBuffer);
bufferDoc
.createPage(595, 842)
.text("Memento Mori", 100, 100)
.text('Memento Mori', 100, 100)
.endPage()
.endPDF(
(outBuffer: Buffer) =>
// $ExpectType Buffer
outBuffer
outBuffer,
);
// $ExpectType Metadata
bufferDoc.metadata;
// $ExpectType PageInfo
bufferDoc.metadata[1];
+63 -90
View File
@@ -1,6 +1,7 @@
// Type definitions for hummus-recipe 1.8
// Project: https://github.com/chunyenHuang/hummusRecipe
// Definitions by: Erik Berreßem <https://github.com/she11sh0cked>
// Sebastian Martinez <https://github.com/sebastinez>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
@@ -8,53 +9,46 @@
declare namespace Recipe {
type CommentOptionsFlag =
| "invisible"
| "hidden"
| "print"
| "nozoom"
| "norotate"
| "noview"
| "readonly"
| "locked"
| "togglenoview";
| 'invisible'
| 'hidden'
| 'print'
| 'nozoom'
| 'norotate'
| 'noview'
| 'readonly'
| 'locked'
| 'togglenoview';
type AnnotSubtype =
| "Text"
| "FreeText"
| "Line"
| "Square"
| "Circle"
| "Polygon"
| "PolyLine"
| "Highlight"
| "Underline"
| "Squiggly"
| "StrikeOut"
| "Stamp"
| "Caret"
| "Ink"
| "FileAttachment"
| "Sound";
| 'Text'
| 'FreeText'
| 'Line'
| 'Square'
| 'Circle'
| 'Polygon'
| 'PolyLine'
| 'Highlight'
| 'Underline'
| 'Squiggly'
| 'StrikeOut'
| 'Stamp'
| 'Caret'
| 'Ink'
| 'FileAttachment'
| 'Sound';
type AnnotOptionsFlag =
| "invisible"
| "hidden"
| "print"
| "nozoom"
| "norotate"
| "noview"
| "readonly"
| "locked"
| "togglenoview";
| 'invisible'
| 'hidden'
| 'print'
| 'nozoom'
| 'norotate'
| 'noview'
| 'readonly'
| 'locked'
| 'togglenoview';
type AnnotOptionsIcon =
| "Comment"
| "Key"
| "Note"
| "Help"
| "NewParagraph"
| "Paragraph"
| "Insert";
type AnnotOptionsIcon = 'Comment' | 'Key' | 'Note' | 'Help' | 'NewParagraph' | 'Paragraph' | 'Insert';
interface RecipeOptions {
version?: number;
@@ -189,6 +183,23 @@ declare namespace Recipe {
rotationOrigin?: number[];
}
interface PageInfo {
pageNumber: number;
mediaBox: number[];
layout: string;
rotate: number;
width: number;
height: number;
size: number[];
offsetX: number;
offsetY: number;
}
interface Metadata {
pages: number;
[PagesIndex: number]: PageInfo;
}
type EndPDFCallback1 = () => any;
type EndPDFCallback2 = (buffer: Buffer) => any;
type EndPDFCallback = EndPDFCallback1 | EndPDFCallback2;
@@ -199,19 +210,11 @@ declare class Recipe {
constructor(buffer: Buffer, options?: Recipe.RecipeOptions);
comment(
text: string,
x: number,
y: number,
options?: Recipe.CommentOptions
): Recipe;
readonly metadata: Recipe.Metadata;
annot(
x: number,
y: number,
subtype: Recipe.AnnotSubtype,
options?: Recipe.AnnotOptions
): Recipe;
comment(text: string, x: number, y: number, options?: Recipe.CommentOptions): Recipe;
annot(x: number, y: number, subtype: Recipe.AnnotSubtype, options?: Recipe.AnnotOptions): Recipe;
appendPage(pdfSrc: string, pages: number | number[]): Recipe;
@@ -219,29 +222,15 @@ declare class Recipe {
registerFont(fontName: string, fontSrcPath: string): Recipe;
image(
imgSrc: string,
x: number,
y: number,
options?: Recipe.ImageOptions
): Recipe;
image(imgSrc: string, x: number, y: number, options?: Recipe.ImageOptions): Recipe;
info(options?: Recipe.InfoOptions): Recipe;
custom(key?: string, value?: string): Recipe;
insertPage(
afterPageNumber: number,
pdfSrc: string,
srcPageNumber: number
): Recipe;
insertPage(afterPageNumber: number, pdfSrc: string, srcPageNumber: number): Recipe;
overlay(
pdfSrc: string,
x: number,
y: number,
options?: Recipe.OverlayOptions
): Recipe;
overlay(pdfSrc: string, x: number, y: number, options?: Recipe.OverlayOptions): Recipe;
createPage(pageWidth: number, pageHeight: number): Recipe;
@@ -253,12 +242,7 @@ declare class Recipe {
split(outputDir: string, prefix: string): Recipe;
text(
text: string,
x: number,
y: number,
options?: Recipe.TextOptions
): Recipe;
text(text: string, x: number, y: number, options?: Recipe.TextOptions): Recipe;
moveTo(x: number, y: number): Recipe;
@@ -268,20 +252,9 @@ declare class Recipe {
polygon(coordinates: number[][], options?: Recipe.PolygonOptions): Recipe;
circle(
x: number,
y: number,
radius: number,
options?: Recipe.CircleOptions
): Recipe;
circle(x: number, y: number, radius: number, options?: Recipe.CircleOptions): Recipe;
rectangle(
x: number,
y: number,
width: number,
height: number,
options?: Recipe.RectangleOptions
): Recipe;
rectangle(x: number, y: number, width: number, height: number, options?: Recipe.RectangleOptions): Recipe;
endPDF(callback?: Recipe.EndPDFCallback): Recipe;
}