npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

canvas-editor-pdf

v0.2.7

Published

pdf exporter to canvas-editor

Downloads

735

Readme

canvas-editor-pdf

Pdf exporter to canvas-editor. This project currently replicates the same code as canvas-editor to be used with the jsPdf library. Making the appropriate specific modifications for jsPdf. I will be updating this repository with all the modifications to canvas-editor so that it does not become obsolete over time.

Usage

As of version 0.1.0, the library is completely decoupled from the editor. You only need to pass the data from the editor - at instantiation or later through the setValue() function. If you use a version prior to 0.1.0, see how to use it below.

npm i canvas-editor-pdf --save

It is recommended to instantiate the library before exporting the PDF, as the UI may crash. This is because jspdf loads fonts synchronously.

Import the class from pdf:

  import { DrawPdf } from 'canvas-editor-pdf'

First you create the library instance:

  const instancePdf = new DrawPdf(
    JSON.parse(JSON.stringify(instance.command.getValue().options)), // make a copy of the editor settings and avoid type conflicts
    instance.command.getValue().data
  )

When you want to export the pdf:

  // If you want to update the data, you need to use await so that internally you can convert latex from svg to png - jspdf does not support svg
  await instancePdf.setValue(instance.command.getValue().data)
  instancePdf.render() // you need to call render to process and create the data within jspdf
  instancePdf.getPdf().save(`test.pdf`) // pdf download

Fonts

At the moment few fonts are supported, jspdf defaults:

  • courier (normal, bold, italic)
  • helvetica (normal, bold, italic)
  • symbol (normal)
  • times (roman, bold, italic)

And also the following fonts:

  • Microsoft Yahei (normal, bold)
  • Arial (normal, bold, itlic)
  • Calibri (normal, bold, itlic)
  • Cambria (normal, bold, itlic)
  • Ink Free (normal)
  • Verdana (normal, bold, itlic)
  • Segoe UI (normal, bold italic)

Other fonts will be added little by little, but if necessary, you can manually add a font by passing the link to the .ttf file:

  // instance.addFont(url, fileName, id, type)
  instance.addFont('https://raw.githubusercontent.com/Hufe921/canvas-editor/refs/heads/feature/pdf/public/font/msyh.ttf', 'Yahei.ttf', 'Yahei', 'normal')

Usage before version 0.1.0

At the moment this project is for internal use in canvas-editor, and it is necessary to create a function within CommandAdapt for the editor to use it. In the future I intend to decouple it so that it can be used independently. That said, first install it:

Now inside the editor in src > editor > core > command > CommandAdapt.ts place this function at the end:

public async pdf(fileName: string) {
    const dataHeader = JSON.parse(JSON.stringify(this.draw.getValue().data.header))
    // You need to update the latex elements from svg to png, since jspdf does not support the svg format
    const updateSrcLatexElements = (elementList: any[]) => {
      elementList.forEach((element) => {
        if (element.laTexSVG) {
          const { scale } = this.options
          const width = element.width! * scale
          const height = element.height! * scale
          svgString2Image(element.laTexSVG, width, height, 'png', (pngData: any) => {
            // pngData is base64 png string
            element.laTexSVG = pngData
          })
        }
      })
    }
    this.draw.getOriginalMainElementList().forEach((elem) => {
      if (elem.laTexSVG) {
        updateSrcLatexElements([elem])
      }
    })
    const dataMain = JSON.parse(JSON.stringify(this.draw.getValue().data.main))
    const dataFooter = JSON.parse(JSON.stringify(this.draw.getValue().data.footer))
    // A timeout is necessary to give time to update the latex elements (in some situations with a lot of data and many laex elements it may be necessary to increase the timeout)
    setTimeout(() => {
      this.draw.render({isLazy: false})
      const drawPdf = new DrawPdf(
        JSON.parse(JSON.stringify(this.draw.getOptions())),
        {
          header: dataHeader,
          main: dataMain,
          footer: dataFooter
        },
        this.draw
      )
      drawPdf.render()
      drawPdf.getPdf().save(${fileName}.pdf)
    }, 1)
}

Inside the editor in src > editor > core > command > Command.ts register the new property:

public pdf: CommandAdapt['pdf']

And inside the constructor define the property with the CommandAdapt function:

this.pdf = adapt.pdf.bind(adapt)

To use the new editor function call it like this:

editor.command.pdf('test')