From 5846a2b8b1fc0124eb45387858d9667ce5eadbbc Mon Sep 17 00:00:00 2001 From: Eric HILLAH Date: Thu, 2 Jul 2015 16:09:29 +0200 Subject: [PATCH 1/3] Add definitions for pdk kit libray --- pdfkit/pdfkit-tests.ts | 137 +++++++++++++++ pdfkit/pdfkit.d.ts | 390 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 527 insertions(+) create mode 100644 pdfkit/pdfkit-tests.ts create mode 100644 pdfkit/pdfkit.d.ts diff --git a/pdfkit/pdfkit-tests.ts b/pdfkit/pdfkit-tests.ts new file mode 100644 index 0000000000..16dc2a4049 --- /dev/null +++ b/pdfkit/pdfkit-tests.ts @@ -0,0 +1,137 @@ +/// + +import {PDFGradient} from "pdfkit/js/gradient"; + +import {PDFRadialGradiant,PDFLinearGradient} from "pdfkit/js/gradient"; + +import * as mtext from "pdfkit/js/mixins/text"; + +import PDFDocument = require("pdfkit"); + +import * as font from "pdfkit/js/mixins/fonts"; +import * as pdfData from "pdfkit/js/data"; +import text = require("pdfkit/js/mixins/text"); + +font.registerFont("Arial"); +text.widthOfString("Kila",{ellipsis:true}); + +var doc = new PDFDocument({compress:false, sizes:[526,525],autoFirstPage:true}); + + + + +doc.addPage({ + margin: 50 +}); + +doc.addPage({ + margins: { + top: 50, + bottom: 50, + left: 72, + right: 72 + } +}); + +doc.info.Title = "Sample"; +doc.info.Author = "kila Mogrosso"; + +// Create basic shapes +doc.moveTo(0,20) + .lineTo(100,160) + .quadraticCurveTo(130,200,150,120) + .lineTo(400,90) + .stroke(); + +//SVG Paths +doc.path("M 0,20 L 100,160 Q 130,200 150,120 C 190,-40 200,200 300,150 L 400,90") + .stroke(); + +//Rectangle shape helper sample +doc.rect(100,200,100,100); + +//polygon +doc.polygon([100,0],[50,100],[50,100]); + + +doc.lineWidth(25); +doc.lineCap('butt').moveTo(50, 20).lineTo(100, 20).stroke(); +doc.lineCap('round').moveTo(150, 20).lineTo(200, 20).stroke(); +doc.lineCap('square').moveTo(250, 20).circle(275, 30, 15).stroke(); +doc.lineJoin('miter').rect(50, 100, 50, 50).stroke(); +doc.lineJoin('round').rect(150, 100, 50, 50).stroke(); +doc.lineJoin('bevel').rect(250, 100, 50, 50).stroke(); + + +doc.circle(100, 50, 50) + .lineWidth(3) + .fillOpacity(0.8) + .fillAndStroke("red", "#900"); + +let grad = doc.linearGradient(50, 0, 150, 100) + .stop(0, 'green') + .stop(1, 'red'); + +doc.rect(50, 0, 100, 100) +.fill(grad); + +doc.circle(100, 50, 50).dash(5, { + space: 10 +}).stroke(); + +let rgrad = doc.radialGradient(300, 50, 0, 300, 50, 50); +rgrad.stop(0, 'orange', 0).stop(1, 'orange', 1); +doc.circle(300, 50, 50) + .fill(rgrad); + +doc.fillColor('red') + .translate(-100, -50) + .scale(0.8); + +doc.path('M 250,75 L 323,301 131,161 369,161 177,301 z') + .fill('non-zero'); + +doc.translate(280, 0) + .path('M 250,75 L 323,301 131,161 369,161 177,301 z') + .fill('even-odd'); + +doc.circle(100,100,100) + .clip(); + +doc.fontSize(25) + .fillColor('blue') + .text('This is a link!', 20, 0); + +let width = doc.widthOfString('This is a link!'); + +let height = doc.currentLineHeight(); + +doc.underline(20, 0, width, height, { + color: 'blue' +}).link(20, 0, width, height, 'http://google.com/'); + +doc.moveDown() + .fillColor('black') + .highlight(20, doc.y, doc.widthOfString('This text is highlighted!'), height) + .text('This text is highlighted!'); + +doc.moveDown() +.strike(20, doc.y, doc.widthOfString('STRIKE!'), height) +.text('STRIKE!'); + +doc.image('images/test.jpeg', 0, 15, { + width: 300 +}).text('Proprotional to width', 0, 0); + +doc.image('images/test.jpeg', 320, 15, { + fit: [100, 100] +}).rect(320, 15, 100, 100).stroke().text('Fit', 320, 0); + +doc.image('images/test.jpeg', 320, 145, { + width: 200, + height: 100 +}).text('Stretch', 320, 130); + +doc.image('images/test.jpeg', 320, 280, { + scale: 0.25 +}).text('Scale', 320, 265); diff --git a/pdfkit/pdfkit.d.ts b/pdfkit/pdfkit.d.ts new file mode 100644 index 0000000000..c9116f7056 --- /dev/null +++ b/pdfkit/pdfkit.d.ts @@ -0,0 +1,390 @@ +// Type definitions for Pdfkit v0.7.1 +// Project: http://pdfkit.org +// Definitions by: Eric Hillah +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module PDFKit { + interface PDFGradient { + new(document: any): PDFGradient ; + stop(pos: number, color?: string|PDFKit.PDFGradient, opacity?: number): PDFGradient; + embed(): void; + apply(): void; + } + + interface PDFLinearGradient extends PDFGradient { + new(document: any, x1: number, y1: number, x2: number, y2: number): PDFLinearGradient; + shader(fn: () => any): any; + opacityGradient(): PDFLinearGradient; + } + + interface PDFRadialGradient extends PDFGradient { + new(document: any, x1: number, y1: number, x2: number, y2: number): PDFRadialGradient; + shader(fn: () => any): any; + opacityGradient(): PDFRadialGradient; + } +} + +declare module PDFKit.Mixins { + + interface AnnotationOption { + Type?: string; + Rect?: any; + Border?: Array; + SubType?: string; + Contents?: string; + Name?: string; + color?: string, + QuadPoints?: Array; + + A?: any; + B?: any; + C?: any; + L?: any; + DA?: string; + } + + interface PDFAnnotation { + annotate(x: number, y: number, w: number, h: number, option: AnnotationOption): TDocument; + note(x: number, y: number, w: number, h: number, content: string, option?: AnnotationOption): TDocument; + link(x: number, y: number, w: number, h: number, url: string, option?: AnnotationOption): TDocument; + highlight(x: number, y: number, w: number, h: number, option?: AnnotationOption): TDocument; + underline(x: number, y: number, w: number, h: number, option?: AnnotationOption): TDocument; + strike(x: number, y: number, w: number, h: number, option?: AnnotationOption): TDocument; + lineAnnotation(x1: number, y1: number, x2: number, y2: number, option?: AnnotationOption): TDocument; + rectAnnotation(x: number, y: number, w: number, h: number, option?: AnnotationOption): TDocument; + ellipseAnnotation(x: number, y: number, w: number, h: number, option?: AnnotationOption): TDocument; + textAnnotation(x: number, y: number, w: number, h: number, text: string, option?: AnnotationOption): TDocument; + } + + interface PDFColor { + fillColor(color: string|PDFGradient, opacity?: number): TDocument; + strokeColor(color: string, opacity?: number): TDocument; + opacity(opacity: number): TDocument; + fillOpacity(opacity: number): TDocument; + strokeOpacity(opacity: number): TDocument; + linearGradient(x1: number, y1: number, x2: number, y2: number): PDFLinearGradient; + radialGradient(x1: number, y1: number, r1: number, x2: number, y2: number, r2: number): PDFRadialGradient; + } + + interface PDFFont { + font(src: string, family?: string, size?: number): TDocument; + fontSize(size: number): TDocument; + currentLineHeight(includeGap?: boolean): number; + registerFont(name: string, src?: string, family?: string): TDocument; + } + + interface ImageOption { + width?: number; + height?: number; + /** Scale percentage */ + scale?: number; + /** Two elements array specifying dimensions(w,h) */ + fit?: number[]; + } + + interface PDFImage { + /** + * Draw an image in PDFKit document. + * No need chainning capabilities + */ + image(src: any, x: number, y: number, options: ImageOption): any; + } + + interface TextOptions { + /** Set to false to disable line wrapping all together */ + lineBreak?: boolean; + /** The width that text should be wrapped to (by default, the page width minus the left and right margin) */ + width?: number; + /** The maximum height that text should be clipped to */ + height?: number; + /** The character to display at the end of the text when it is too long. Set to true to use the default character. */ + ellipsis?: boolean|string; + /** the number of columns to flow the text into */ + columns?: number; + /** the amount of space between each column (1/4 inch by default) */ + columnGap?: number; + /** The amount in PDF points (72 per inch) to indent each paragraph of text */ + indent?: number; + /** the amount of space between each paragraph of text */ + paragrahGap?: number; + /** the amount of space between each line of text */ + lineGap?: number; + /** the amount of space between each word in the text */ + wordSpacing?: number; + /** the amount of space between each character in the text */ + characterSpacing?: number; + /** whether to fill the text (true by default) */ + fill?: boolean; + /** whether to stroke the text */ + stroke?: boolean; + /** A URL to link this text to (shortcut to create an annotation) */ + link?: string; + /** whether to underline the text */ + underline?: boolean; + /** whether to strike out the text */ + strike?: boolean; + /**whether the text segment will be followed immediately by another segment. Useful for changing styling in the middle of a paragraph. */ + continued?: boolean; + } + + interface PDFText { + lineGap(lineGap: number): TDocument; + moveDown(line?: number): TDocument; + moveUp(line?: number): TDocument; + text(text: string, x?: number, y?: number, options?: TextOptions): TDocument; + text(text: string, options?: TextOptions): TDocument; + widthOfString(text: string, options?: TextOptions): number; + heightOfString(text: string, options?: TextOptions): number; + list(list: Array, x?: number, y?: number, options?: TextOptions): TDocument; + list(list: Array, options?: TextOptions): TDocument; + } + + interface PDFVector { + + save(): TDocument; + restore(): TDocument; + closePath(): TDocument; + lineWidth(w: number): TDocument; + lineCap(c: string): TDocument; + lineJoin(j: string): TDocument; + miterLimit(m: any): TDocument; + dash(length: number, option: any): TDocument; + undash(): TDocument; + moveTo(x: number, y: number): TDocument; + lineTo(x: number, y: number): TDocument; + bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): TDocument; + quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): TDocument; + rect(x: number, y: number, w: number, h: number): TDocument; + roundRect(x: number, y: number, w: number, h: number, r?: number): TDocument; + ellipse(x: number, y: number, r1: number, r2?: number): TDocument; + circle(x: number, y: number, raduis: number): TDocument; + polygon(...points: number[][]): TDocument; + path(path: string): TDocument; + fill(color: string|PDFKit.PDFGradient, rule?: string): TDocument; + stroke(color?: string|PDFKit.PDFGradient): TDocument; + fillAndStroke(fillColor: string, strokeColor?: string, rule?: string): TDocument; + clip(rule?: string): TDocument; + transform(m11: number, m12: number, m21: number, m22: number, dx: number, dy: number): TDocument; + translate(x: number, y: number): TDocument; + rotate(angle: number, options?: { origin?: number[] }): TDocument; + scale(xFactor: number, yFactor?: number, options?: { origin?: number[] }): TDocument; + } +} + +declare module PDFKit { + /** + * PDFKit data + */ + interface PDFData { + new (data: any[]): PDFData; + readByte(): any; + writeByte(byte: any): void; + byteAt(index: number): any; + readBool(): boolean; + writeBool(val: boolean): boolean; + readUInt32(): number; + writeUInt32(val: number): void; + readInt32(): number; + writeInt32(val: number): void; + readUInt16(): number; + writeUInt16(val: number): void; + readInt16(): number; + writeInt16(val: number): void; + readString(length: number): string; + writeString(val: string): void; + stringAt(pos: number, length: number): string; + readShort(): number; + writeShort(val: number): void; + readLongLong(): number; + writeLongLong(val: number): void; + readInt(): number; + writeInt(val: number): void; + slice(start: number, end: number): any[]; + read(length: number): any[]; + write(bytes: any[]): void; + } +} + +declare module "pdfkit/js/data" { + var PDFKitData: PDFKit.PDFData; + export = PDFKitData; +} + +declare module PDFKit { + interface DocumentInfo { + Producer?: string, + Creator?: string, + CreationDate?: Date, + Title?: string, + Author?: string, + Keywords?: string, + ModDate?: Date + } + + interface PDFDocumentOptions { + compress?: boolean; + info?: DocumentInfo; + autoFirstPage?: boolean; + sizes?: number[]; + margin?: { top: number, left: number, bottom: number, right: number }|number; + + bufferPages?: boolean; + } + + interface PDFDocument extends NodeJS.ReadableStream, + Mixins.PDFAnnotation, Mixins.PDFColor, Mixins.PDFImage, + Mixins.PDFText, Mixins.PDFVector, Mixins.PDFFont { + /** + * PDF Version + */ + version: number; + /** + * Wheter streams should be compressed + */ + compress: boolean; + /** + * PDF document Metadata + */ + info: DocumentInfo; + /** + * Options for the document + */ + options: PDFDocumentOptions; + /** + * Represent the current page. + */ + page: PDFPage; + + x: number; + y: number; + + new (options?: PDFDocumentOptions): PDFDocument; + + addPage(options?: PDFDocumentOptions): PDFDocument; + bufferedPageRanges(): { start: number; count: number }; + switchToPage(n?: number): PDFPage; + flushPages(): void; + ref(data: {}): PDFKitReference; + addContent(data: any): PDFDocument + /** + * Deprecated + */ + write(fileName: string, fn: any): void; + /** + * Deprecated. Throws exception + */ + output(fn: any): void; + end(): void; + toString(): string; + } +} + +declare module "pdfkit" { + var doc: PDFKit.PDFDocument; + export = doc; +} + +declare module "pdfkit/js/gradient" { + var gradient : { + PDFGradient: PDFKit.PDFGradient, + PDFLinearGradient: PDFKit.PDFLinearGradient, + PDFRadialGradiant: PDFKit.PDFRadialGradient + } + + export = gradient; +} + +declare module PDFKit { + /** + * Represent a single page in the PDF document + */ + interface PDFPage { + size: string; + layout: string; + margin: { top: number, left: number, bottom: number, right: number }|number; + width: number; + height: number; + document: PDFDocument; + content: PDFKitReference; + + /** + * The page dictionnary + */ + dictionary: PDFKitReference; + + fonts: any; + xobjects: any; + ext_gstates: any; + patterns: any; + annotations: any; + + maxY(): number; + write(chunk: any): void; + end(): void; + } +} + +declare module "pdfkit/js/page" { + var PDFKitPage: PDFKit.PDFPage + + export = PDFKitPage +} + +declare module PDFKit { + /** PDFReference - represents a reference to another object in the PDF object heirarchy */ + class PDFKitReference { + id: number; + gen: number; + deflate:any; + compress: boolean; + uncompressedLength: number; + chunks: any[]; + data: { Font?: any, XObject?: any, ExtGState?: any, Pattern: any, Annots: any }; + document: PDFDocument; + + constructor(document: PDFDocument, id: number, data: {}); + initDeflate(): void; + write(chunk: any): void; + end(chunk: any): void; + finalize(): void; + toString(): string; + } +} + +declare module "pdfkit/js/reference" { + var PDFKitReference: PDFKit.PDFKitReference; + + export = PDFKitReference; +} + +declare module "pdfkit/js/mixins/annotations" { + var PDFKitAnnotation: PDFKit.Mixins.PDFAnnotation; + export = PDFKitAnnotation; +} + +declare module "pdfkit/js/mixins/color" { + var PDFKitColor: PDFKit.Mixins.PDFColor; + export = PDFKitColor; +} + +declare module "pdfkit/js/mixins/fonts" { + var PDFKitFont: PDFKit.Mixins.PDFFont; + export = PDFKitFont; +} + +declare module "pdfkit/js/mixins/images" { + var PDFKitImage: PDFKit.Mixins.PDFImage; + export = PDFKitImage; +} + +declare module "pdfkit/js/mixins/text" { + var PDFKitText: PDFKit.Mixins.PDFText; + export = PDFKitText; +} + +declare module "pdfkit/js/mixins/vector" { + var PDFKitVector: PDFKit.Mixins.PDFVector; + export = PDFKitVector; +} From 3ef4e12c363b5ea43065d2ab6bd6770a4a055aae Mon Sep 17 00:00:00 2001 From: Eric HILLAH Date: Thu, 2 Jul 2015 16:44:48 +0200 Subject: [PATCH 2/3] Update to pass travis ci --- pdfkit/pdfkit-tests.ts | 13 ++++++------- pdfkit/pdfkit.d.ts | 28 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/pdfkit/pdfkit-tests.ts b/pdfkit/pdfkit-tests.ts index 16dc2a4049..94d7349763 100644 --- a/pdfkit/pdfkit-tests.ts +++ b/pdfkit/pdfkit-tests.ts @@ -1,15 +1,16 @@ /// -import {PDFGradient} from "pdfkit/js/gradient"; +import PDFGradient = require("pdfkit/js/gradient"); -import {PDFRadialGradiant,PDFLinearGradient} from "pdfkit/js/gradient"; +let PDFRadialGradiant = PDFGradient.PDFRadialGradiant; +let PDFLinearGradient = PDFGradient.PDFLinearGradient; -import * as mtext from "pdfkit/js/mixins/text"; +import mtext = require("pdfkit/js/mixins/text"); import PDFDocument = require("pdfkit"); -import * as font from "pdfkit/js/mixins/fonts"; -import * as pdfData from "pdfkit/js/data"; +import font = require("pdfkit/js/mixins/fonts"); +import pdfData = require("pdfkit/js/data"); import text = require("pdfkit/js/mixins/text"); font.registerFont("Arial"); @@ -18,8 +19,6 @@ text.widthOfString("Kila",{ellipsis:true}); var doc = new PDFDocument({compress:false, sizes:[526,525],autoFirstPage:true}); - - doc.addPage({ margin: 50 }); diff --git a/pdfkit/pdfkit.d.ts b/pdfkit/pdfkit.d.ts index c9116f7056..d4e1beeecb 100644 --- a/pdfkit/pdfkit.d.ts +++ b/pdfkit/pdfkit.d.ts @@ -35,7 +35,7 @@ declare module PDFKit.Mixins { SubType?: string; Contents?: string; Name?: string; - color?: string, + color?: string; QuadPoints?: Array; A?: any; @@ -214,13 +214,13 @@ declare module "pdfkit/js/data" { declare module PDFKit { interface DocumentInfo { - Producer?: string, - Creator?: string, - CreationDate?: Date, - Title?: string, - Author?: string, - Keywords?: string, - ModDate?: Date + Producer?: string; + Creator?: string; + CreationDate?: Date; + Title?: string; + Author?: string; + Keywords?: string; + ModDate?: Date; } interface PDFDocumentOptions { @@ -228,7 +228,7 @@ declare module PDFKit { info?: DocumentInfo; autoFirstPage?: boolean; sizes?: number[]; - margin?: { top: number, left: number, bottom: number, right: number }|number; + margin?: { top: number; left: number; bottom: number; right: number }|number; bufferPages?: boolean; } @@ -288,9 +288,9 @@ declare module "pdfkit" { declare module "pdfkit/js/gradient" { var gradient : { - PDFGradient: PDFKit.PDFGradient, - PDFLinearGradient: PDFKit.PDFLinearGradient, - PDFRadialGradiant: PDFKit.PDFRadialGradient + PDFGradient: PDFKit.PDFGradient; + PDFLinearGradient: PDFKit.PDFLinearGradient; + PDFRadialGradiant: PDFKit.PDFRadialGradient; } export = gradient; @@ -303,7 +303,7 @@ declare module PDFKit { interface PDFPage { size: string; layout: string; - margin: { top: number, left: number, bottom: number, right: number }|number; + margin: { top: number; left: number; bottom: number; right: number }|number; width: number; height: number; document: PDFDocument; @@ -341,7 +341,7 @@ declare module PDFKit { compress: boolean; uncompressedLength: number; chunks: any[]; - data: { Font?: any, XObject?: any, ExtGState?: any, Pattern: any, Annots: any }; + data: { Font?: any; XObject?: any; ExtGState?: any; Pattern: any; Annots: any }; document: PDFDocument; constructor(document: PDFDocument, id: number, data: {}); From dff6169755272581f837ff47eddea2fb09e8b8d3 Mon Sep 17 00:00:00 2001 From: Eric HILLAH Date: Thu, 2 Jul 2015 16:54:13 +0200 Subject: [PATCH 3/3] change let to var --- pdfkit/pdfkit-tests.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pdfkit/pdfkit-tests.ts b/pdfkit/pdfkit-tests.ts index 94d7349763..f60a00b1c9 100644 --- a/pdfkit/pdfkit-tests.ts +++ b/pdfkit/pdfkit-tests.ts @@ -2,8 +2,8 @@ import PDFGradient = require("pdfkit/js/gradient"); -let PDFRadialGradiant = PDFGradient.PDFRadialGradiant; -let PDFLinearGradient = PDFGradient.PDFLinearGradient; +var PDFRadialGradiant = PDFGradient.PDFRadialGradiant; +var PDFLinearGradient = PDFGradient.PDFLinearGradient; import mtext = require("pdfkit/js/mixins/text"); @@ -67,7 +67,7 @@ doc.circle(100, 50, 50) .fillOpacity(0.8) .fillAndStroke("red", "#900"); -let grad = doc.linearGradient(50, 0, 150, 100) +var grad = doc.linearGradient(50, 0, 150, 100) .stop(0, 'green') .stop(1, 'red'); @@ -78,7 +78,7 @@ doc.circle(100, 50, 50).dash(5, { space: 10 }).stroke(); -let rgrad = doc.radialGradient(300, 50, 0, 300, 50, 50); +var rgrad = doc.radialGradient(300, 50, 0, 300, 50, 50); rgrad.stop(0, 'orange', 0).stop(1, 'orange', 1); doc.circle(300, 50, 50) .fill(rgrad); @@ -101,9 +101,9 @@ doc.fontSize(25) .fillColor('blue') .text('This is a link!', 20, 0); -let width = doc.widthOfString('This is a link!'); +var width = doc.widthOfString('This is a link!'); -let height = doc.currentLineHeight(); +var height = doc.currentLineHeight(); doc.underline(20, 0, width, height, { color: 'blue'