Skip to content

Instantly share code, notes, and snippets.

@AmirTugi
Last active July 17, 2019 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AmirTugi/5c0a2ad2b8566c938e7bb9dfb36105f7 to your computer and use it in GitHub Desktop.
Save AmirTugi/5c0a2ad2b8566c938e7bb9dfb36105f7 to your computer and use it in GitHub Desktop.
import TeaSchool from 'tea-school';
import * as pug from 'pug';
import * as path from 'path';
import {PDFOptions} from 'puppeteer';
import {Options as SassOptions} from 'node-sass';
(async () => {
/********************************
* STYLE OPTIONS *
********************************/
const styleOptions: SassOptions = {
// Get relative path from cwd to the desired file
file: path.resolve(__dirname, 'invoice-pdf.scss'),
};
/********************************
* TEMPLATE FILE PATH *
********************************/
// Get relative path from cwd to the desired file
const htmlTemplatePath = path.resolve(__dirname, 'invoice-pdf.template.pug');
/********************************
* TEMPLATE OPTIONS *
********************************/
const htmlTemplateOptions: pug.LocalsObject = {
invoice: new Invoice(...),
};
/********************************
* PDF FILE OPTIONS *
********************************/
const pdfOptions: PDFOptions = {
// Output path will be relative
path: path.resolve(__dirname, 'output', 'invoice.pdf'),
format: 'A4',
};
/********************************
* PUTTING IT TOGETHER *
********************************/
const teaSchoolOptions: TeaSchool.GeneratePdfOptions = {
styleOptions,
htmlTemplatePath,
htmlTemplateOptions,
pdfOptions,
};
/**************************************************************
* GENERATED PDF AS A BUFFER AND ALSO SAVED TO A FILE *
**************************************************************/
const pdfFile = await TeaSchool.generatePdf(teaSchoolOptions);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment