Node.jsでpdfkitを使用してPDFを作成する方法

Node.jsでpdfkitを使用してPDFを作成する方法

pdfkit install

pdfkitをインストールします。

$ npm init -y
$ npm i pdfkit
$ touch index.mjs

index.mjs

import PDFDocument from 'pdfkit'
import fs from 'node:fs'

const doc = new PDFDocument()
doc.pipe(fs.createWriteStream('mypdf.pdf'))
doc.end()

実行します。

$ node index.mjs

mypdf.pdfが作成されます。

文字を追加

text()メソッドで文字追加します。moveDown()メソッドで1行下に移動します。

引数を指定するとその行数移動します。※moveDown(3)…3行下に移動する

import PDFDocument from 'pdfkit'
import fs from 'node:fs'

const doc = new PDFDocument()
doc.text('hello world!')
doc.moveDown()
doc.text('hello world!')
doc.moveDown(2)
doc.text('hello world!')
doc.pipe(fs.createWriteStream('mypdf.pdf'))
doc.end()

文字の方向指定

文字はalign(方向)を指定することができます。

import PDFDocument from 'pdfkit'
import fs from 'node:fs'

const doc = new PDFDocument()
doc.text('hello world!',{align: 'left'})
doc.text('hello world!',{align: 'center'})
doc.text('hello world!',{align: 'right'})
doc.text('hello world!',{align: 'justify'})
doc.pipe(fs.createWriteStream('mypdf.pdf'))
doc.end()

作成したpdfです。

mypdf

文字の色指定

fillColor()メソッドで文字の色を指定することができます。Cは大文字になります。

import PDFDocument from 'pdfkit'
import fs from 'node:fs'

const doc = new PDFDocument()
doc.fillColor('green').text('hello world!')
doc.pipe(fs.createWriteStream('mypdf.pdf'))
doc.end()

作成したpdfです。

mypdf

文字ハイライト

highlight()メソッドで文字をハイライトします。

highlight()メソッドの引数は5つあり、以下の通りです。

引数 内容
第一引数 x
第二引数 y
第三引数 width
第四引数 height
第四引数 AnnotationOption

index.mjs

import PDFDocument from 'pdfkit'
import fs from 'node:fs'

const doc = new PDFDocument()
const str = 'hello world!'
doc.highlight(
  doc.x,
  doc.y,
  doc.widthOfString(str),
  doc.currentLineHeight()
).text(str)
doc.pipe(fs.createWriteStream('mypdf.pdf'))
doc.end()

作成したpdfです。

mypdf

罫線(矩形)

罫線(矩形)はrect()メソッドで表すことができます。

rect()メソッドの引数は4つあり、以下の通りです。

引数 内容
第一引数 x
第二引数 y
第三引数 width
第四引数 height

index.mjs

import PDFDocument from 'pdfkit'
import fs from 'node:fs'

const doc = new PDFDocument()
doc.text('hello world!',{align: 'left'})
doc.text('hello world!',{align: 'center'})
doc.text('hello world!',{align: 'right'})
doc.text('hello world!',{align: 'justify'})
doc.rect(doc.x, 0, 500, doc.y).stroke()
doc.pipe(fs.createWriteStream('mypdf.pdf'))
doc.end()

作成したpdfです。

mypdf

罫線(矩形)の線の色を指定する場合は、stroke()メソッドの引数に色指定を行います。

import PDFDocument from 'pdfkit'
import fs from 'node:fs'

const doc = new PDFDocument()
doc.text('hello world!',{align: 'left'})
doc.text('hello world!',{align: 'center'})
doc.text('hello world!',{align: 'right'})
doc.text('hello world!',{align: 'justify'})
doc.rect(doc.x, 0, 500, doc.y).stroke('green')
doc.pipe(fs.createWriteStream('mypdf.pdf'))
doc.end()

作成したpdfです。

mypdf

日本語対応

日本語対応するためにipag.ttfをルートディレクトリに配置します。

new PDFDocument()のコンストラクタ引数で{font:パス名}を渡します。これでipag.ttfをデフォルトフォントとして指定します。

index.mjs

import PDFDocument from 'pdfkit'
import fs from 'node:fs'

const doc = new PDFDocument({font: './ipag.ttf'})
doc.text('hello world!')
doc.moveDown()
doc.text('日本語')
doc.pipe(fs.createWriteStream('mypdf.pdf'))
doc.end()

メタデータ追加

作成したpdfにメタデータを追加できます。new PDFDocument()のコンストラクタ引数でinfoを指定します。

項目 内容
Title タイトル
Author 作成者
Subject ドキュメントの件名
Keywords ドキュメントに関連するキーワード
CreationDate 作成された日付 (PDFKitによって自動的に追加される)
ModDate ドキュメントが最後に修正された日付
import PDFDocument from 'pdfkit'
import fs from 'node:fs'

const doc = new PDFDocument({
  info: {
      Title: 'Sample Title',
      Author: 'Takahashi',
      Subject: 'Subject Title',
      Keywords: 'pdfkit'
    },
  font: './ipag.ttf'}
)
doc.text('hello world!')
doc.pipe(fs.createWriteStream('mypdf.pdf'))
doc.end()

作成したpdfのメタデータを確認します。

Node.jsでpdfkitを使用してPDFを作成する方法

余白設定

pdfの余白設定をします。new PDFDocument()のコンストラクタ引数でmarginsを指定します。

index.mjs

import PDFDocument from 'pdfkit'
import fs from 'node:fs'

const doc = new PDFDocument({
    margins: {
    top: 0,
    bottom: 0,
    left: 0,
    right: 0
}})
doc.text('hello world!'.repeat(50))
doc.pipe(fs.createWriteStream('mypdf.pdf'))
doc.end()

余白0のpdfです。

mypdf

余白未設定時のデフォルトは1inch(72point)になります。

画像追加

image()メソッドを使用してpdfファイル内に画像を差し込みます。

ルートディレクトリ直下にsample.pngを配置します。この画像をpdfに差し込みます。

jpeg,png形式をサポートしているようです。

index.mjs

import PDFDocument from 'pdfkit'
import fs from 'node:fs'

const doc = new PDFDocument()
doc.text('hello world!')
doc.image('sample.png')
doc.pipe(fs.createWriteStream('mypdf.pdf'))
doc.end()

画像の幅と高さを指定することも可能です。

doc.image('sample2.png', { width: 30, height:20 })

ページ追加

addPage()メソッドでページ追加することができます。

index.mjs

import PDFDocument from 'pdfkit'
import fs from 'node:fs'

const doc = new PDFDocument({font: './ipag.ttf',size: 'A4'})
doc.text('hello world!') // 1ページ目に出力
doc.addPage({ size: 'A4' })
doc.text('日本語') // 2ページ目に出力
doc.pipe(fs.createWriteStream('mypdf.pdf'))
doc.end()

作成したpdfです。

mypdf

参考サイト

Paper Sizes
Images in PDFKit

https://pdfkit.org/docs/guide.pdf

コメント

株式会社CONFRAGE ITソリューション事業部をもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む

タイトルとURLをコピーしました