diff --git a/packages/markdown-pdf/README.md b/packages/markdown-pdf/README.md index 3b60f9ca..2d021c1c 100644 --- a/packages/markdown-pdf/README.md +++ b/packages/markdown-pdf/README.md @@ -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 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/. diff --git a/packages/markdown-pdf/index.js b/packages/markdown-pdf/index.js index 2d478f71..be50ac76 100644 --- a/packages/markdown-pdf/index.js +++ b/packages/markdown-pdf/index.js @@ -21,3 +21,4 @@ module.exports.PdfTransformer = require('./lib/PdfTransformer'); module.exports.ToPdfMake = require('./lib/ToPdfMake'); +module.exports.DefaultFonts = require('./lib/pdfmakeutil').defaultFonts; diff --git a/packages/markdown-pdf/src/PdfTransformer.js b/packages/markdown-pdf/src/PdfTransformer.js index b432b477..af730f49 100644 --- a/packages/markdown-pdf/src/PdfTransformer.js +++ b/packages/markdown-pdf/src/PdfTransformer.js @@ -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)); } } diff --git a/packages/markdown-pdf/src/PdfTransformerBase.js b/packages/markdown-pdf/src/PdfTransformerBase.js index 07ab714c..c2e7265c 100644 --- a/packages/markdown-pdf/src/PdfTransformerBase.js +++ b/packages/markdown-pdf/src/PdfTransformerBase.js @@ -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) { @@ -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 }); @@ -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); } }