Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add jsPDF setProperties to save method and documentProperties to options #635

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ The basic workflow of html2pdf.js tasks (enforced by the prereq system) is:
| output | type, options, src | Routes to the appropriate `outputPdf` or `outputImg` method based on specified `src` (`'pdf'` (default) or `'img'`). |
| outputPdf | type, options | Sends `type` and `options` to the jsPDF object's `output` method, and returns the result as a Promise (use `.then` to access). See the [jsPDF source code](https://rawgit.com/MrRio/jsPDF/master/docs/jspdf.js.html#line992) for more info. |
| outputImg | type, options | Returns the specified data type for the image as a Promise (use `.then` to access). Supported types: `'img'`, `'datauristring'`/`'dataurlstring'`, and `'datauri'`/`'dataurl'`. |
| save | filename | Saves the PDF object with the optional filename (creates user download prompt). |
| save | filename, documentProperties | Saves the PDF object (creates user download prompt) with the optional filename and document properties (see [jsPDF DocumentProperties](https://github.com/parallax/jsPDF/blob/2d9a91916471f1fbe465dbcdc05db0cf22d720ec/types/index.d.ts#L677)). Ex. `html2pdf().save('output.pdf', { title: 'My PDF' })` |
| set | opt | Sets the specified properties. See [Options](#options) below for more details. |
| get | key, cbk | Returns the property specified in `key`, either as a Promise (use `.then` to access), or by calling `cbk` if provided. |
| then | onFulfilled, onRejected | Standard Promise method, with `this` re-bound to the Worker, and with added progress-tracking (see [Progress](#progress) below). Note that `.then` returns a `Worker`, which is a subclass of Promise. |
Expand All @@ -146,11 +146,12 @@ html2pdf.js can be configured using an optional `opt` parameter:
```js
var element = document.getElementById('element-to-print');
var opt = {
margin: 1,
filename: 'myfile.pdf',
image: { type: 'jpeg', quality: 0.98 },
html2canvas: { scale: 2 },
jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
margin: 1,
filename: 'myfile.pdf',
image: { type: 'jpeg', quality: 0.98 },
html2canvas: { scale: 2 },
jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
documentProperties: { title: 'My PDF' }
};

// New Promise-based usage:
Expand All @@ -171,6 +172,7 @@ The `opt` parameter has the following optional fields:
|enableLinks |boolean |`true` |If enabled, PDF hyperlinks are automatically added ontop of all anchor tags. |
|html2canvas |object |`{ }` |Configuration options sent directly to `html2canvas` ([see here](https://html2canvas.hertzen.com/configuration) for usage).|
|jsPDF |object |`{ }` |Configuration options sent directly to `jsPDF` ([see here](http://rawgit.com/MrRio/jsPDF/master/docs/jsPDF.html) for usage).|
|documentProperties |object |`{ }` | Configuration options applied directly to `jsPDF` pdf ([see here](https://www.rotisedapsales.com/snr/cloud_staging/website/jsPDF-master/docs/global.html#setProperties) for usage).

### Page-breaks

Expand Down
19 changes: 16 additions & 3 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ Worker.template = {
image: { type: 'jpeg', quality: 0.95 },
enableLinks: true,
html2canvas: {},
jsPDF: {}
jsPDF: {},
documentProperties: {}
}
};

Expand Down Expand Up @@ -271,16 +272,28 @@ Worker.prototype.outputImg = function outputImg(type, options) {
});
};

Worker.prototype.save = function save(filename) {
Worker.prototype.save = function save(filename, documentProperties) {
// Set up function prerequisites.
var prereqs = [
function checkPdf() { return this.prop.pdf || this.toPdf(); }
];

var setProps = {};
if (filename) {
setProps.filename = filename;
}
if (documentProperties && Object.keys(documentProperties).length) {
setProps.documentProperties = documentProperties
}

// Fulfill prereqs, update the filename (if provided), and save the PDF.
return this.thenList(prereqs).set(
filename ? { filename: filename } : null
Object.keys(setProps).length ? setProps : null,
).then(function save_main() {
if (this.opt.documentProperties && Object.keys(this.opt.documentProperties).length) {
this.prop.pdf.setProperties({...this.opt.documentProperties});
}

this.prop.pdf.save(this.opt.filename);
});
};
Expand Down