Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@pdftron/canvas-to-pdf

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@pdftron/canvas-to-pdf

CanvasToPDF can create vector quality PDF images using the canvas API.

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

CanvasToPDF

CanvasToPDF can create vector quality PDF images using the canvas API.

How it works

Internally, CanvasToPDF uses modified versions of canvas2pdf and PDFKit to call on actual PDF drawing methods. CanvasToPDF has several notable improvements from canvas2pdf in that it has no issues calling fill and stroke consecutively and that reading canvas properties do not lead to errors. Thus, you can create vectorized PDFs such as the one below.

Screen Shot 2022-08-18 at 10 23 15 AM

CanvasToPDF internally manages a mock 2d canvas context. Thus, you only need to pass a draw function calling on canvas commands and not the canvas itself.

Dependencies

  • canvas2pdf
  • PDFKit
  • blob-stream

Usage

CanvasToPDF currently only supports client-side usage. You can use this library similar to the manner below.

Sample Code for Using CanvasToPDF

import canvasToPDF from "@pdftron/canvas-to-pdf";
import saveAs from "./FileSaver";

const draw = (ctx) => {
  // canvas methods
};

canvasToPDF(draw, { width: 100, height: 150 }).then((res) => {
  saveAs(res, "example.pdf", true);
});

The canvasToPDF function returns a blob containing vector graphics based PDF file, but since it is difficult to check that the blob has the expected images drawn you can use FileSaver to download an actual PDF as seen in the code above.

Sample Code for Drawing Vector PDF

const draw = (ctx) => {
  for (let i = 0; i < 12; i++) {
    for (let j = 0; j < 12; j++) {
      ctx.strokeStyle = `rgb(
    0,
    ${Math.floor(255 - 42.5 * i)},
    ${Math.floor(255 - 42.5 * j)})`;
      ctx.beginPath();
      ctx.arc(25 + j * 40, 25 + i * 40, 15, 0, Math.PI * 2, true);
      ctx.stroke();
    }
  }
};

The above code will draw multiple circles with gradient borders:

case2

License

MIT

Notes

  • Some canvas 2d context methods are not implemented yet
  • Drawing line widths of less than 1 is not supported
  • Currently only supports 3 default fonts: Helvetica, Courier, and Times-Roman

FAQs

Package last updated on 02 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc