pdf2svgでPDFをSVGに変換する

はじめに

pdf2svgは、PDF文書をSVG(Scalable Vector Graphics)ファイルに変換する軽量なコマンドラインツールである。
PDFの各ページを個別のSVGファイルとして出力することができ、ベクターグラフィックスとして高品質な変換を実現する。 今回はこれを試してみる。

環境

Windows 11 Professional
WSL2 (Ubuntu 24.04 LTS)

インストール

Ubuntu/Debianでのインストール

Ubuntuやその他のDebian系ディストリビューションでは、aptパッケージマネージャーを使用してpdf2svgを簡単にインストールできる。

sudo apt update
sudo apt install pdf2svg
ログ
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  crack-common libdouble-conversion3 libgl1-amber-dri libglapi-mesa libllvm17t64 libmd4c0 libpcre2-16-0
  libqt5core5t64 libqt5dbus5t64 libqt5gui5t64 libqt5network5t64 libqt5qml5 libqt5qmlmodels5 libqt5quick5
  libqt5svg5 libqt5waylandclient5 libqt5waylandcompositor5 libqt5widgets5t64 libqt5x11extras5
  libxcb-damage0 libxcb-xinerama0 libxcb-xinput0 mesa-utils-bin qt5-gtk-platformtheme qttranslations5-l10n
  qtwayland5 xbitmaps
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  pdf2svg
0 upgraded, 1 newly installed, 0 to remove and 44 not upgraded.
Need to get 8578 B of archives.
After this operation, 31.7 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu noble/universe amd64 pdf2svg amd64 0.2.3-5build3 [8578 B]
Fetched 8578 B in 1s (16.2 kB/s)
Selecting previously unselected package pdf2svg.
(Reading database ... 127623 files and directories currently installed.)
Preparing to unpack .../pdf2svg_0.2.3-5build3_amd64.deb ...
Unpacking pdf2svg (0.2.3-5build3) ...
Setting up pdf2svg (0.2.3-5build3) ...
Processing triggers for man-db (2.12.0-4build2) ...

依存関係

pdf2svgは以下のライブラリに依存している

  • libc6 (>= 2.34) - GNU C Library
  • libcairo2 (>= 1.2.4) - Cairo 2D vector graphics library
  • libglib2.0-0t64 (>= 2.12.0) - GLib library of C routines
  • libpoppler-glib8t64 (>= 0.18.0) - PDF rendering library

これらの依存関係は通常、aptでpdf2svgをインストールする際に自動的にインストールされる。

その他のディストリビューション

  • Fedora/RHEL/CentOS

    dnf install pdf2svg
    yum install pdf2svg
  • Arch Linux

    pacman -S pdf2svg
  • macOS (Homebrew)

    brew install pdf2svg

インストールの確認

インストール後、正常に動作することを確認できる

# man で確認
man pdf2svg

正常にインストールされていれば、使用方法が表示される。

ログ
PDF2SVG(1)                               General Commands Manual                               PDF2SVG(1)

NAME
       pdf2svg - PDF to SVG convertor

SYNOPSIS
       pdf2svg pdffile svgfile [page number]

DESCRIPTION
       pdf2svg  is  a tiny command-line utility using Cairo and Poppler to convert PDF documents into SVG
       files.  Multi-page PDF can be split up to one SVG per page by passing a file naming specification.

       The program does not follow the usual GNU command line syntax.

       The first argument is the source PDF file, the second argument is the filename of the  output  SVG
       file (or a specification, see below for details).

OPTIONS
       The third parameter is optional and serves as a page selector. If omitted it defaults to the first
       page  of the passed PDF.  If passed it must be a valid page label (typically it is a value such as
       "iii" or "3").

   all
       This special selector causes the program to iterate over all pages in the PDF.  Because it  cannot
       save  multiple  pages  into  one single SVG the second parameter is expected to contain a sensible
       file specification:

       pdf2svg document.pdf output-page%d.svg all

       The usual format modificators work as well: output-page%02d.svg will give  you  output-page00.svg,
       output-page01.svg, etc.

AUTHOR
       pdf2svg   was  written  by  David  Barton  <[email protected]>  and  Matthew  Flaschen
       <[email protected]>.

       This manual page was written by Philipp Kern <[email protected]>, for the Debian project  (but  may
       be used by others).

                                              July 16, 2008                                    PDF2SVG(1)

基本的な使い方

基本コマンド

pdf2svgの基本的な構文は以下の通りである

pdf2svg input.pdf output.svg [page]
  • input.pdf: 変換したいPDFファイル
  • output.svg: 出力するSVGファイル名
  • page: 変換するページ番号(省略可能)

pdf2svgを使ってみる

以前の記事で使用した下記のPDFを利用する。
mysql-article

PDFの全ページを変換

pdf2svg mysql_article.pdf output-%d.svg all

この コマンドは mysql_article.pdf の全ページを output-1.svg, output-2.svg, … のように連番で出力する。
%d は自動的にページ番号に置換される。

以下で出力された。

-rw-r--r--  1 kbushi kbushi 1.3M May 24 17:34  output-1.svg
-rw-r--r--  1 kbushi kbushi 2.7M May 24 17:34  output-2.svg
-rw-r--r--  1 kbushi kbushi 2.7M May 24 17:34  output-3.svg
-rw-r--r--  1 kbushi kbushi 3.2M May 24 17:34  output-4.svg
-rw-r--r--  1 kbushi kbushi 4.2M May 24 17:34  output-5.svg
-rw-r--r--  1 kbushi kbushi 2.2M May 24 17:34  output-6.svg
-rw-r--r--  1 kbushi kbushi 331K May 24 17:34  output-7.svg

特定のページのみを変換

pdf2svg mysql_article.pdf page3.svg 3

この例では、PDFの3ページ目のみを page3.svg として出力する。

以下で出力された。

-rw-r--r-- 1 kbushi kbushi 2.7M May 24 17:35 page3.svg

ファイル名の連番指定

複数ページのPDFで、特定の桁数で連番を指定する

# 3桁の連番(001, 002, 003...)
pdf2svg document.pdf page-%03d.svg all

# ゼロ埋めなしの連番
pdf2svg document.pdf page-%d.svg all

トラブルシューティング

フォントが正しく表示されない

PDFに埋め込まれていないフォントがある場合、代替フォントが使用される可能性がある。 以下で回避できる可能性がある。

  • PDF作成時にフォントを埋め込む
  • システムに必要なフォントをインストールする

大きなPDFファイルの処理が遅い

大きなPDFファイルや多数のページを含むファイルの変換には時間がかかる場合がある。

# 特定のページ範囲のみを変換
for i in {1..10}; do
    pdf2svg large_document.pdf page-$i.svg $i
done

参考

おわりに

今回はPDFSVGに変換するpdf2svgを使ってみた。
軽量で使いやすい変換ツールで、コマンドライン操作に慣れていれば、バッチ処理やスクリプトでの自動化も容易に実現できそう。
PDFSVGに変換したいなってときは思い出して積極的に使っていきたい。

Hugo で構築されています。
テーマ StackJimmy によって設計されています。