Skip to content

The custom font doesn`t apply when use into Table Cell #1635

Open

Description

@kvandake

Bug Report

Description of the problem

When I use custom font in CellOptions pdfkit ignore property because th source code has a bug in line
https://github.com/foliojs/pdfkit/blob/master/lib/table/render.js#L86,

I think resolution be as use text function:

const doc = new PDFDocument({
  margin: 50,
  size: 'A4',
  layout: 'landscape',
});

doc.font(...)

P.S. I use typescript with @types/pdfkit and customFont doesn`t allow in CellOptions

Image

Code sample

      const tableHeaders = [
        {
          text: 'Header 1',
          // temporary fix
          font: { src: PDF_FONT_BOLD },
          fontSize: 12,
         // temporary fix
          customFont: true,
        },
        ...days.map((day) => ({
          text: day.getDate().toString(),
        })),
      ] as TableCell[];

Your environment

  • pdfkit version: 0.17.1
  • @types/pdfkit: 0.17.0
  • Node version: v20.12.2
  • Browser version (if applicable): Edge
  • Operating System: Windows 11

Activity

  1. joernroeder commented on Jul 20, 2025

    @joernroeder
  2. monolithed commented on Dec 4, 2025

    @monolithed

    Here's my workaround:

    interface PDFKitFont {
        size: number;
        src?: string | Buffer;
    }
    
    interface PDFKitPadding {
        top?: number;
        right?: number;
        bottom?: number;
        left?: number;
    }
    
    type PDFKitColumnStyle = {
        width?: number;
        align?: 'left' | 'center' | 'right'
    };
    
    interface PDFKitTextStyle extends PDFKitColumnStyle {
        padding?: PDFKitPadding | number;
        text: string;
        color?: string;
        textColor?: string;
        font?: string | PDFKitFont;
    }
    
    interface PDFKitRowStyles {
        border?: boolean | string | number | (string | number)[];
        padding?: number | [number, number] | [number, number, number, number] | PDFKitPadding;
    }
    
    interface PDFKitTableOptions {
        x?: number;
        y?: number;
        debug?: boolean;
        defaultStyle?: PDFKitRowStyles;
        prepareHeader?: () => void;
        prepareRow?: () => void;
        rowStyles?: PDFKitRowStyles;
        columnStyles?: (number | '*' | PDFKitColumnStyle)[];
        data: Array<PDFKitTextStyle | string>[];
    }
    
    interface PDFTable {
        table(options: PDFKitTableOptions): this;
    }
    
    declare namespace PDFKit {
        interface PDFDocument extends PDFTable {}
    }
    
    declare module 'pdfkit/js/pdfkit.standalone' {
        const document: PDFKit.PDFDocument;
    
        export = document;
    }
  3. monolithed commented on Dec 5, 2025

    @monolithed

    And probably #1647

  4. kshep92 commented on Sep 6, 2026

    @kshep92

    @joernroeder I saw that in the file you referenced, the code only acknowledges font.src. Should there also be a path that checks for font.family or font as a string so that users can reference a built-in font in a more natural way?

    For example:

    { text: "Cell text", font: "Helvetica-Bold" }
    // Or
    { text: "Cell text", font: { family: "Helvetica-Bold" } }
    

    If so, I'll create a PR for this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions