Skip to content

random-guys/pdfmills

Repository files navigation

pdfmills

A simple DSL wrapper around pdfkit

What's inside

  • Elements:
    • Text or Paragraphs
    • Images
  • Layouts:
    • Block
    • Flex
    • RatioFlex
    • EqualFlex
  • Spacing:
    • LineBreaks for vertical spaces
    • Margin
    • Padding
  • Automatic page management

How to install

yarn add @random-guys/pdfmills

or

npm i @random-guys/pdfmills

Get Started

import { block, Text } from "@random-guys/pdfmills";

// arrange elements vertically
const divStyle: BlockStyle = {
  display: "block",
  margin: 0
};

const flexStyle: FlexStyle = {
  display: "flex"
};

div(
  { ...divStyle, margin: 4 },
  row({
    style: flexStyle,
    elements: [
      col(p("START DATE"), ["right"], 60),
      col(p("ACCOUNT NUMBER", alignRight), ["right", "left"], 100),
      col(p("INCOME", alignRight), ["left"], 60)
    ]
  }),
  row({
    style: flexStyle,
    elements: [
      col(p("12/03/07", { ...headerFont, align: "left" }), ["right"], 60),
      col(p("0087567843", headerFont), ["right", "left"], 100),
      col(p("₦250,000", headerFont), ["left"], 60)
    ]
  }),
  br(20),
  row({
    style: flexStyle,
    elements: [
      col(p("END DATE"), ["right"], 60),
      col(p("BALANCE", alignRight), ["right", "left"], 100),
      col(p("INCOME", alignRight), ["left"], 60)
    ]
  }),
  row({
    style: flexStyle,
    elements: [
      col(p("12/03/07", { ...headerFont, align: "left" }), ["right"], 60),
      col(p("₦560,000", headerFont), ["right", "left"], 100),
      col(p("₦25,000", headerFont), ["left"], 60)
    ]
  })
);

// arrange elements horizontally
const headerFont: FontStyle = { ...alignRight, fontColor: [130, 134, 145] };
row({
  style: flexStyle,
  elements: [
    col(p("12/03/07", { ...headerFont, align: "left" }), ["right"], 60),
    col(p("0087567843", headerFont), ["right", "left"], 100),
    col(p("₦250,000", headerFont), ["left"], 60)
  ]
});

// load the element
const elements = [firstRow, br(50), secondRow, br(40), thirdRow];

// write the elements to the buffer
render(context, elements);

// you can write the buffer to the file stream
save(context.raw, "output.pdf");

// you can also upload the file to Azure blob storage
import { Blobber } from "@random-guys/blobber";
import uuid from "uuid/v4";

const tempName = `${uuid()}.pdf`;
const uploader = new Blobber<any>("reports");
context.raw.end();
return await uploader.streamLocalFile(tempName, this.context.raw);

Checkout the examples folder for real life examples.