Skip to content

The PDF file created by createPDFStream becomes larger in size, and createWriteStream is very slow #2615

Description

@948764321

Version of node-canvas

4.0.0-rc3

Version of node

v20.19.0

What operating system does the problem occur on?

Linux

Steps to reproduce

Create a large number of text-to-PDF conversions to achieve the desired result.

Additional information/comments

No response

Activity

  1. chearon commented on Aug 25, 2026

    @chearon
    Collaborator

    Can you provide a minimal script to reproduce the problem? Also, does the issue happen in 3.2.3 or just 4.0.0-rc3?

  2. 948764321 commented on Sep 2, 2026

    @948764321
    Author

    Can you provide a minimal script to reproduce the problem? Also, does the issue happen in 3.2.3 or just 4.0.0-rc3?

    // Minimal repro: node-canvas 4.0.0-rc3 PDF export is slow and huge vs 3.2.3
    // Run:  npm i canvas@3.2.3     && node repro-issue.js
    //       npm i canvas@4.0.0-rc3 && node repro-issue.js
    'use strict'
    const fs = require('fs')
    const { createCanvas } = require('canvas')
    const version = require('canvas/package.json').version
    
    const PAGES = 10          // A4 pages
    const LINES = 45          // body text lines per page
    const BODY = '汉字性能测试:落霞与孤鹜齐飞,秋水共长天一色,共二十五个全角字符。'
    const out = `out-${version}.pdf`
    
    async function main() {
      const t0 = Date.now()
      const canvas = createCanvas(595.28, 841.89, 'pdf')
      const ctx = canvas.getContext('2d')
      ctx.textDrawingMode = 'glyph'
    
      for (let p = 0; p < PAGES; p++) {
        if (p > 0) ctx.addPage(595.28, 841.89)
        ctx.fillStyle = '#ffffff'
        ctx.fillRect(0, 0, 595.28, 841.89)
        ctx.fillStyle = '#000000'
        ctx.font = 'bold 16px "sans-serif"'
        ctx.fillText(`Heading ${p + 1}`, 72, 80)
        ctx.font = '10.5px "sans-serif"'
        for (let i = 0; i < LINES; i++) ctx.fillText(`${BODY} (${i + 1})`, 72, 120 + i * 16)
        ctx.strokeStyle = '#333333'
        ctx.lineWidth = 0.5
        ctx.strokeRect(72, 780, 451, 60)
      }
      const drawMs = Date.now() - t0
    
      const t1 = Date.now()
      await new Promise((res, rej) => {
        const ws = fs.createWriteStream(out)
        const s = canvas.createPDFStream()
        s.on('error', rej); ws.on('error', rej); ws.on('finish', res)
        s.pipe(ws)
      })
      const exportMs = Date.now() - t1
    
      console.log(JSON.stringify({
        version, pages: PAGES, linesPerPage: LINES,
        drawMs, exportMs, totalMs: drawMs + exportMs,
        pdfMB: +(fs.statSync(out).size / 1048576).toFixed(2),
      }, null, 2))
    }
    
    main().catch(e => { console.error(e); process.exit(1) })

    result

    {
      "version": "4.0.0-rc3",
      "pages": 10,
      "linesPerPage": 45,
      "drawMs": 2422,
      "exportMs": 3240,
      "totalMs": 5662,
      "pdfMB": 26.91
    }
    
    {
      "version": "3.2.3",
      "pages": 10,
      "linesPerPage": 45,
      "drawMs": 113,
      "exportMs": 29,
      "totalMs": 142,
      "pdfMB": 0.076
    }
    
    
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions