Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ pdfDoc
.endPDF(()=>{ /* done! */ });
```

### Create a new PDF as a Buffer

```javascript
const HummusRecipe = require('hummus-recipe');
const pdfDoc = new HummusRecipe(Buffer.from('new'), null, {
version: 1.6,
author: 'John Doe',
title: 'Hummus Recipe',
subject: 'A brand new PDF'
});

const pdfBuffer = pdfDoc
.createPage('letter-size')
.endPage()
.endPDF();
```

## Modify an existing PDF

```javascript
Expand Down
5 changes: 3 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ declare namespace Recipe {
title?: string;
subject?: string;
keywords?: string[];
[key: string]: unknown;
}

interface CommentOptions {
Expand Down Expand Up @@ -187,7 +188,7 @@ declare namespace Recipe {
}

declare class Recipe {
constructor(src: string, output: string, options?: Recipe.RecipeOptions);
constructor(src: string|Buffer, output: string, options?: Recipe.RecipeOptions);

constructor(buffer: Buffer, options?: Recipe.RecipeOptions);

Expand Down Expand Up @@ -275,7 +276,7 @@ declare class Recipe {
options?: Recipe.RectangleOptions
): Recipe;

endPDF(callback?: Recipe.EndPDFCallback): Recipe;
endPDF(callback?: Recipe.EndPDFCallback): Buffer|undefined;
}

export = Recipe;
20 changes: 16 additions & 4 deletions lib/Recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const streams = require('memory-streams');
* @desc Create a pdfDoc
* @namespace
* @constructor
* @param {string} src - The file path or Buffer of the src file.
* @param {string|Buffer} src - The file path or Buffer of the src file.
* @param {string} output - The path of the output file.
* @param {Object} [options] - The options for pdfDoc
* @param {number} [options.version] - The pdf version
Expand All @@ -27,21 +27,22 @@ class Recipe {
this.src = src;
// detect the src is Buffer or not
this.isBufferSrc = this.src instanceof Buffer;
this.isNewPDF = (!this.isBufferSrc && src.toLowerCase() === 'new');
this.isNewPDF = src.toString().toLowerCase() === 'new';
this.encryptOptions = this._getEncryptOptions(options, this.isNewPDF);
this.options = Object.assign({}, options, this.encryptOptions);
this.current = {};
this.current.defaultFontSize = 14;

if (this.isBufferSrc) {
this.outStream = new streams.WritableStream();
this.output = output;
this.output = this.isNewPDF ? null : output;
} else {
this.output = output || src;
if (this.src) {
this.filename = path.basename(this.src);
}
}

this.muhammara = muhammara;
this.logFile = 'muhammara-error.log';

Expand Down Expand Up @@ -69,7 +70,8 @@ class Recipe {

_createWriter() {
if (this.isNewPDF) {
this.writer = muhammara.createWriter(this.output,
this.writer = muhammara.createWriter(
this.isBufferSrc ? new muhammara.PDFStreamForResponse(this.outStream) : this.output,
Object.assign( {}, this.encryptOptions, {
version: this._getVersion(this.options.version)
})
Expand Down Expand Up @@ -201,6 +203,7 @@ class Recipe {
* @function
* @memberof Recipe
* @param {function} callback - The callback function.
* @returns {Buffer|undefined}
*/
endPDF(callback) {
this._writeInfo();
Expand Down Expand Up @@ -266,7 +269,16 @@ class Recipe {
} else {
return callback();
}
} else {
if (this.isBufferSrc) {
if (this.output) {
return this.output
} else {
return this.outStream.toBuffer()
}
}
}

}

/**
Expand Down
Loading