How to use pdfkit - 10 common examples

To help you get started, we’ve selected a few pdfkit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nrkno / core-icons / bin / build.js View on Github external
function svgtopdf (el, options, pdf) {
  if (typeof el === 'string') {
    const svg = svgson.parseSync(fs.readFileSync(el, 'utf-8'))
    const [, , width, height] = svg.attributes.viewBox.split(' ').map(Number)
    const scale = 24 / Math.min(width, height)
    const pdfdoc = new PDFDocument({ size: [width * scale, height * scale] })
    pdfdoc.scale(scale)
    pdfdoc.pipe(fs.createWriteStream(el.replace('svg', 'pdf')))
    svgtopdf(svg, { fill: '#000' }, pdfdoc)
    pdfdoc.end()
  } else {
    for (const node of el.children) {
      const fillr = (node.attributes['fill-rule'] || 'nonzero').replace(/-*(zero|odd)$/, '-$1')
      const color = (val) => String(val).replace(/currentColor/i, options.color).replace(/none/i, '')
      const float = (key) => Number(node.attributes[key]) || 0
      const style = {}

      // Style
      pdf.fillColor(style.fill = color(node.attributes['fill'] || options.fill || options.color))
      pdf.strokeColor(style.stroke = color(node.attributes['stroke'] || options.stroke || 'none'))
      pdf.lineWidth(style.lineWidth = Number(node.attributes['stroke-width']) || options.lineWidth || 1)
      pdf.lineJoin(style.lineJoin = node.attributes['stroke-linejoin'] || options.lineJoin || 'miter')
github diegomura / react-pdf / src / mount.js View on Github external
ReactUpdates.batchedUpdates(() => {
    let doc = new pdf();

    const transaction = ReactUpdates.ReactReconcileTransaction.getPooled();

    doc.pipe(fs.createWriteStream(filePath));

    transaction.perform(() => {
      // Starts mounting recursive process
      component.mountComponent(transaction, rootId, null, {doc});

      doc.end();

      if (callback) {
        callback(component.getPublicInstance());
      }
    });
    ReactUpdates.ReactReconcileTransaction.release(transaction);
github cennznet / runanode / app / main / ipc / generatePaperWalletChannel.js View on Github external
// Helpers
      const printMnemonic = (index) => `${index + 1}. ${mnemonics[index]}`;
      const readAssetSync = (p: string) => fs.readFileSync(p.startsWith('..') ? path.join(__dirname, p) : p);

      // Generate QR image for wallet address
      const qrCodeImage = qr.imageSync(address, {
        type: 'png',
        size: 10,
        ec_level: 'L',
        margin: 0
      });
      const textColor = '#040C40';
      const width = 595.28;
      const height = 841.98;
      const doc = new PDFDocument({
        size: [width, height],
        margins: {
          bottom: 0,
          left: 0,
          right: 0,
          top: 0,
        },
        info: {
          Title: messages.infoTitle,
          Author: messages.infoAuthor,
        }
      });
      try {
        // font family
        // const fontBuffer = readAssetSync(paperWalletFontPath);
        // doc.font(fontBuffer);
github cennznet / runanode / app / main / ipc / generatePaperWalletChannel.js View on Github external
// Helpers
      const printMnemonic = (index) => `${index + 1}. ${mnemonics[index]}`;
      const readAssetSync = (p: string) => fs.readFileSync(p.startsWith('..') ? path.join(__dirname, p) : p);

      // Generate QR image for wallet address
      const qrCodeImage = qr.imageSync(address, {
        type: 'png',
        size: 10,
        ec_level: 'L',
        margin: 0
      });
      const textColor = '#040C40';
      const width = 595.28;
      const height = 841.98;
      const doc = new PDFDocument({
        size: [width, height],
        margins: {
          bottom: 0,
          left: 0,
          right: 0,
          top: 0,
        },
        info: {
          Title: messages.infoTitle,
          Author: messages.infoAuthor,
        }
      });

      try {
        // font family
        // const fontBuffer = readAssetSync(paperWalletFontPath);
github foliojs / textkit / temp.js View on Github external
Path,
  Rect,
  LayoutEngine,
  AttributedString,
  Container,
  Attachment
} from '@textkit/textkit';

const path = new Path();

path.rect(30, 30, 300, 400);

// const exclusion = new Path();
// exclusion.circle(140, 160, 50);

const doc = new PDFDocument();
doc.pipe(fs.createWriteStream('out.pdf'));

path.toFunction()(doc);
// exclusion.toFunction()(doc);

doc.stroke('green');
doc.stroke();

const string = AttributedString.fromFragments([
  {
    string: 'Lorem ipsum dolor sit amet, ',
    attributes: {
      font: 'Arial',
      fontSize: 14,
      bold: true,
      align: 'justify',
github NathanaelA / fluentreports / lib / fluentReports.pdfkit.js View on Github external
return arr;
};

PDFParser.prototype.consumeExtraWhiteSpace = function() {
    var chr;

    while ((chr = this._data[this._offset]) === 10 || chr === 13 || chr === 0 || chr === 9 || chr === 12 || chr === 32 ) { this._offset++; }
};


// ---------------------------------------------------------------------------
// Add our functions to PDFKit
// ---------------------------------------------------------------------------

//noinspection JSUnresolvedVariable
pdfkit.prototype.getEmptyPageStats = function() {
    this.emptyPageCountData = [
        this.page._imageCount,
        this.page.content.uncompressedLength,
        this.page.resources.uncompressedLength,
        this.page.dictionary.uncompressedLength
    ];
};

//noinspection JSUnresolvedVariable
pdfkit.prototype.isEmptyPage = function() {
   // Verify we have a valid page, and a valid pdfkit page
   if (!this.page || !this.page.document) {
        return false;
   }

    if (!Array.isArray(this.emptyPageCountData)) {
github NathanaelA / fluentreports / lib / fluentReports.pdfkit.js View on Github external
}
         if (this.page.dictionary && !this.page.dictionary.offset) {
             this._waiting--;
         }
         if (this.page.resources && !this.page.resources.offset) {
             this._waiting--;
         }
         this.page = { end: function() {} };
     }
};

// ---------------------------------------------------------------------------
// We need to Hijack the PDFKit Document End and _Finalize to support deletion and auto-deletion
// ---------------------------------------------------------------------------
//noinspection JSUnresolvedVariable
pdfkit.prototype._PKIEnd = pdfkit.prototype.end;

//noinspection JSUnresolvedVariable
pdfkit.prototype.end = function() {
    if (this.isEmptyPage()) {
        this.deletePage();
    }
    this._PKIEnd();
};

//noinspection JSUnresolvedVariable
pdfkit.prototype._PKIFinalize = pdfkit.prototype._finalize;

//noinspection JSUnresolvedVariable
pdfkit.prototype._finalize = function(fn) {
    var i = 0;
    while (i < this._offsets.length) {
github NathanaelA / fluentreports / lib / fluentReports.pdfkit.js View on Github external
//noinspection JSUnresolvedVariable
pdfkit.prototype._PKIEnd = pdfkit.prototype.end;

//noinspection JSUnresolvedVariable
pdfkit.prototype.end = function() {
    if (this.isEmptyPage()) {
        this.deletePage();
    }
    this._PKIEnd();
};

//noinspection JSUnresolvedVariable
pdfkit.prototype._PKIFinalize = pdfkit.prototype._finalize;

//noinspection JSUnresolvedVariable
pdfkit.prototype._finalize = function(fn) {
    var i = 0;
    while (i < this._offsets.length) {
        // Eliminate any NULL Offsets from deleted pages
        if (this._offsets[i] === null) {
           this._offsets.splice(i, 1);
           continue;
        }
        i++;
    }
    this._PKIFinalize(fn);
};

//noinspection JSUnresolvedVariable
/**
 * This imports all the PDF pages
 * @param data - this is the PDF document buffer
github NathanaelA / fluentreports / lib / fluentReports.pdfkit.js View on Github external
// ---------------------------------------------------------------------------
// Add our functions to PDFKit
// ---------------------------------------------------------------------------

//noinspection JSUnresolvedVariable
pdfkit.prototype.getEmptyPageStats = function() {
    this.emptyPageCountData = [
        this.page._imageCount,
        this.page.content.uncompressedLength,
        this.page.resources.uncompressedLength,
        this.page.dictionary.uncompressedLength
    ];
};

//noinspection JSUnresolvedVariable
pdfkit.prototype.isEmptyPage = function() {
   // Verify we have a valid page, and a valid pdfkit page
   if (!this.page || !this.page.document) {
        return false;
   }

    if (!Array.isArray(this.emptyPageCountData)) {
        this.emptyPageCountData = [0,18,0,0];
    }

   if (this.page._imageCount > this.emptyPageCountData[0]) {    // 0;
       return false;
   }

    // A Transform is placed in the content on a new page and its length is 18
   if (this.page.content.uncompressedLength > this.emptyPageCountData[1]) { // 18
       return false;
github NathanaelA / fluentreports / lib / fluentReports.pdfkit.js View on Github external
// ---------------------------------------------------------------------------
// We need to Hijack the PDFKit Document End and _Finalize to support deletion and auto-deletion
// ---------------------------------------------------------------------------
//noinspection JSUnresolvedVariable
pdfkit.prototype._PKIEnd = pdfkit.prototype.end;

//noinspection JSUnresolvedVariable
pdfkit.prototype.end = function() {
    if (this.isEmptyPage()) {
        this.deletePage();
    }
    this._PKIEnd();
};

//noinspection JSUnresolvedVariable
pdfkit.prototype._PKIFinalize = pdfkit.prototype._finalize;

//noinspection JSUnresolvedVariable
pdfkit.prototype._finalize = function(fn) {
    var i = 0;
    while (i < this._offsets.length) {
        // Eliminate any NULL Offsets from deleted pages
        if (this._offsets[i] === null) {
           this._offsets.splice(i, 1);
           continue;
        }
        i++;
    }
    this._PKIFinalize(fn);
};

//noinspection JSUnresolvedVariable

pdfkit

A PDF generation library for Node.js

MIT
Latest version published 4 days ago

Package Health Score

90 / 100
Full package analysis