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
29 changes: 29 additions & 0 deletions packages/markdown-pdf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,35 @@ const outputStream = fs.createWriteStream(`./test.pdf`);
pdfTransformer.toPdf(ciceroMarkJson, options, outputStream );
```

### Unicode Font Support

> Note: The default Liberation fonts support Latin characters only. For Unicode support (Cyrillic, Chinese, Arabic, etc.), provide custom fonts via the `fonts` parameter.

Example:
``` javascript
const { PdfTransformer, DefaultFonts } = require('@accordproject/markdown-pdf');

// Extend default fonts with a Unicode-capable font
const customFonts = {
...DefaultFonts,
NotoSans: {
normal: '/path/to/NotoSans-Regular.ttf',
bold: '/path/to/NotoSans-Bold.ttf',
italics: '/path/to/NotoSans-Italic.ttf',
bolditalics: '/path/to/NotoSans-BoldItalic.ttf'
}
};

const pdfmakeDOM = await PdfTransformer.ciceroMarkToPdfMake(ciceroMarkJson);
pdfmakeDOM.defaultStyle = { fontSize: 12, font: 'NotoSans', lineHeight: 1.5 };

const pdfBuffer = await PdfTransformer.pdfMakeToPdfBuffer(
pdfmakeDOM,
null, // progress callback
customFonts // your custom fonts
);
```

## License <a name="license"></a>
Accord Project source code files are made available under the Apache License, Version 2.0 (Apache-2.0), located in the LICENSE file. Accord Project documentation files are made available under the Creative Commons Attribution 4.0 International License (CC-BY-4.0), available at http://creativecommons.org/licenses/by/4.0/.

Expand Down
1 change: 1 addition & 0 deletions packages/markdown-pdf/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@

module.exports.PdfTransformer = require('./lib/PdfTransformer');
module.exports.ToPdfMake = require('./lib/ToPdfMake');
module.exports.DefaultFonts = require('./lib/pdfmakeutil').defaultFonts;
5 changes: 3 additions & 2 deletions packages/markdown-pdf/src/PdfTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ class PdfTransformer extends PdfTransformerBase {
* Converts a pdfmake DOM to a PDF Buffer
* @param {*} input - pdfmake DOM (JSON)
* @param {*} progressCallback - a callback function used during pdf emit
* @param {object} [fonts] - optional custom fonts object for pdfmake (defaults to defaultFonts)
* @return {*} a pdf buffer
*/
static async pdfMakeToPdfBuffer(input, progressCallback) {
return PdfTransformerBase.toBuffer(input, PdfTransformerBase.pdfMakeToPdfStreamWithCallback(progressCallback));
static async pdfMakeToPdfBuffer(input, progressCallback, fonts) {
return PdfTransformerBase.toBuffer(input, PdfTransformerBase.pdfMakeToPdfStreamWithCallback(progressCallback, fonts));
}
}

Expand Down
13 changes: 8 additions & 5 deletions packages/markdown-pdf/src/PdfTransformerBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ class PdfTransformerBase {
/**
* Converts a pdfmake DOM to a PDF Buffer
* @param {*} progressCallback - a callback function used during pdf emit
* @param {object} [fonts] - optional custom fonts object for pdfmake (defaults to defaultFonts)
* @return {*} a function from input and stream, adding the pdf to the stream
*/
static pdfMakeToPdfStreamWithCallback(progressCallback) {
static pdfMakeToPdfStreamWithCallback(progressCallback, fonts) {
return (input, outputStream) => {

if (!input.defaultStyle) {
Expand All @@ -113,8 +114,9 @@ class PdfTransformerBase {
};
}

// The Pdf printer
const printer = new PdfPrinter(defaultFonts);
// The Pdf printer - use custom fonts if provided, otherwise use defaultFonts
const fontsToUse = fonts || defaultFonts;
const printer = new PdfPrinter(fontsToUse);

// Printing to stream
const pdfDoc = printer.createPdfKitDocument(input, { progressCallback });
Expand All @@ -127,9 +129,10 @@ class PdfTransformerBase {
* Converts a pdfmake DOM to a PDF Buffer
* @param {*} input - pdfmake DOM (JSON)
* @param {*} outputStream - the output stream
* @param {object} [fonts] - optional custom fonts object for pdfmake (defaults to defaultFonts)
*/
static async pdfMakeToPdfStream(input, outputStream) {
return PdfTransformerBase.pdfMakeToPdfStreamWithCallback()(input, outputStream);
static async pdfMakeToPdfStream(input, outputStream, fonts) {
return PdfTransformerBase.pdfMakeToPdfStreamWithCallback(null, fonts)(input, outputStream);
}
}

Expand Down