はじめに
ExcelからPDFに変換できるコマンドを試してみる。
環境
Windows 10 Professional
WSL2 - (Ubuntu22.04 LTS)
LibreOffice 7.3.7.2 30(Build:2)
準備
Excelファイルは下記を使用する。
calendar.xlsx
LibreOfficeのインストール
$ sudo apt-get install libreoffice
libreoffice
単体のみ入れられるかと思ったけど、依存パッケージが多いみたい。
https://ask.libreoffice.org/t/topic/33337
https://wiki.documentfoundation.org/Development/BuildingOnLinux/ja
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
cpp cpp-11 default-jre dictionaries-common emacsen-common firebird3.0-common firebird3.0-common-doc
firebird3.0-server-core firebird3.0-utils fonts-crosextra-caladea fonts-crosextra-carlito fonts-dejavu
fonts-dejavu-extra fonts-liberation fonts-liberation2 fonts-linuxlibertine fonts-noto-core fonts-noto-extra
fonts-noto-mono fonts-noto-ui-core fonts-opensymbol fonts-sil-gentium fonts-sil-gentium-basic gcc-11-base
gstreamer1.0-gl gstreamer1.0-gtk3 hunspell-en-us libabsl20210324 libabw-0.1-1 libatk-wrapper-java
libatk-wrapper-java-jni libauthen-sasl-perl libboost-filesystem1.74.0 libboost-iostreams1.74.0 libboost-locale1.74.0
libboost-thread1.74.0 libbsh-java libcdr-0.1-1 libclone-perl libclucene-contribs1v5 libclucene-core1v5 libcolamd2
libdata-dump-perl libe-book-0.1-1 libel-api-java libencode-locale-perl libeot0 libepubgen-0.1-1 libetonyek-0.1-1
libexttextcat-2.0-0 libexttextcat-data libfbclient2 libfile-basedir-perl libfile-desktopentry-perl
libfile-listing-perl libfile-mimeinfo-perl libfont-afm-perl libfreehand-0.1-1 libgif7 libgpgme11 libgpgmepp6
libgraphene-1.0-0 libgstreamer-gl1.0-0 libgtkd-3-0 libharfbuzz-icu0 libhsqldb1.8.0-java libhtml-form-perl
libhtml-format-perl libhtml-parser-perl libhtml-tagset-perl libhtml-tree-perl libhttp-cookies-perl
libhttp-daemon-perl libhttp-date-perl libhttp-message-perl libhttp-negotiate-perl libhunspell-1.7-0 libib-util
libio-html-perl libio-socket-ssl-perl libio-stringy-perl libipc-system-simple-perl libisl23 libjsp-api-java
liblangtag-common liblangtag1 liblibreoffice-java libllvm11 libltdl7 liblwp-mediatypes-perl
liblwp-protocol-https-perl libmailtools-perl libmhash2 libmpc3 libmspub-0.1-1 libmwaw-0.3-3 libmysqlclient21
libmythes-1.2-0 libnet-dbus-perl libnet-http-perl libnet-smtp-ssl-perl libnet-ssleay-perl libodfgen-0.1-1
liborcus-0.17-0 liborcus-parser-0.17-0 libpagemaker-0.0-0 libphobos2-ldc-shared98 libpq5 libraptor2-0 librasqal3
librdf0 libreoffice-base libreoffice-base-core libreoffice-base-drivers libreoffice-calc libreoffice-common
libreoffice-core libreoffice-draw libreoffice-gnome libreoffice-gtk3 libreoffice-impress libreoffice-java-common
libreoffice-math libreoffice-nlpsolver libreoffice-report-builder libreoffice-report-builder-bin
libreoffice-script-provider-bsh libreoffice-script-provider-js libreoffice-script-provider-python
libreoffice-sdbc-firebird libreoffice-sdbc-hsqldb libreoffice-sdbc-mysql libreoffice-sdbc-postgresql
libreoffice-style-colibre libreoffice-style-elementary libreoffice-style-yaru libreoffice-wiki-publisher
libreoffice-writer librevenge-0.0-0 libservlet-api-java libservlet3.1-java libsuitesparseconfig5 libtie-ixhash-perl
libtimedate-perl libtommath1 libtry-tiny-perl libuno-cppu3 libuno-cppuhelpergcc3-3 libuno-purpenvhelpergcc3-3
libuno-sal3 libuno-salhelpergcc3-3 libunoloader-java liburi-perl libvisio-0.1-1 libvte-2.91-0 libvte-2.91-common
libvted-3-0 libwebsocket-api-java libwpd-0.10-10 libwpg-0.3-3 libwps-0.4-4 libwww-perl libwww-robotrules-perl
libx11-protocol-perl libxft2 libxml-parser-perl libxml-twig-perl libxml-xpathengine-perl libxmlsec1 libxmlsec1-nss
libxv1 libxxf86dga1 libyajl2 lp-solve mysql-common openjdk-11-jre perl-openssl-defaults python3-uno tilix
tilix-common uno-libs-private ure ure-java x11-utils x11-xserver-utils xdg-utils
Suggested packages:
cpp-doc gcc-11-locales ispell | aspell | hunspell wordlist firebird3.0-server firebird3.0-doc hunspell
openoffice.org-hunspell | openoffice.org-core libdigest-hmac-perl libgssapi-perl java-virtual-machine
libhsqldb1.8.0-java-gcj libcrypt-ssleay-perl raptor2-utils rasqal-utils librdf-storage-postgresql
librdf-storage-mysql librdf-storage-sqlite librdf-storage-virtuoso redland-utils cups-bsd firefox | firefox-esr
| thunderbird ghostscript gpa hyphen-hyphenation-patterns imagemagick | graphicsmagick-imagemagick-compat
libreoffice-grammarcheck libreoffice-help libreoffice-l10n libreoffice-librelogo myspell-dictionary mythes-thesaurus
openclipart-libreoffice pstoedit unixodbc gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly
gstreamer1.0-plugins-bad gstreamer1.0-libav libsane1 libofficebean-java libjtds-java libsqliteodbc | tdsodbc
| odbc-mdbtools ocl-icd-libopencl1 | mesa-opencl-icd | beignet-opencl-icd libreoffice-evolution seahorse
libreofficekit-data bluez default-mysql-server | virtual-mysql-server postgresql mediawiki libsub-name-perl
libbusiness-isbn-perl libauthen-ntlm-perl libunicode-map8-perl libunicode-string-perl xml-twig-tools python-nautilus
mesa-utils nickle cairo-5c xorg-docs-core
The following NEW packages will be installed:
cpp cpp-11 default-jre dictionaries-common emacsen-common firebird3.0-common firebird3.0-common-doc
firebird3.0-server-core firebird3.0-utils fonts-crosextra-caladea fonts-crosextra-carlito fonts-dejavu
fonts-dejavu-extra fonts-liberation fonts-liberation2 fonts-linuxlibertine fonts-noto-core fonts-noto-extra
fonts-noto-mono fonts-noto-ui-core fonts-opensymbol fonts-sil-gentium fonts-sil-gentium-basic gcc-11-base
gstreamer1.0-gl gstreamer1.0-gtk3 hunspell-en-us libabsl20210324 libabw-0.1-1 libatk-wrapper-java
libatk-wrapper-java-jni libauthen-sasl-perl libboost-filesystem1.74.0 libboost-iostreams1.74.0 libboost-locale1.74.0
libboost-thread1.74.0 libbsh-java libcdr-0.1-1 libclone-perl libclucene-contribs1v5 libclucene-core1v5 libcolamd2
libdata-dump-perl libe-book-0.1-1 libel-api-java libencode-locale-perl libeot0 libepubgen-0.1-1 libetonyek-0.1-1
libexttextcat-2.0-0 libexttextcat-data libfbclient2 libfile-basedir-perl libfile-desktopentry-perl
libfile-listing-perl libfile-mimeinfo-perl libfont-afm-perl libfreehand-0.1-1 libgif7 libgpgme11 libgpgmepp6
libgraphene-1.0-0 libgstreamer-gl1.0-0 libgtkd-3-0 libharfbuzz-icu0 libhsqldb1.8.0-java libhtml-form-perl
libhtml-format-perl libhtml-parser-perl libhtml-tagset-perl libhtml-tree-perl libhttp-cookies-perl
libhttp-daemon-perl libhttp-date-perl libhttp-message-perl libhttp-negotiate-perl libhunspell-1.7-0 libib-util
libio-html-perl libio-socket-ssl-perl libio-stringy-perl libipc-system-simple-perl libisl23 libjsp-api-java
liblangtag-common liblangtag1 liblibreoffice-java libllvm11 libltdl7 liblwp-mediatypes-perl
liblwp-protocol-https-perl libmailtools-perl libmhash2 libmpc3 libmspub-0.1-1 libmwaw-0.3-3 libmysqlclient21
libmythes-1.2-0 libnet-dbus-perl libnet-http-perl libnet-smtp-ssl-perl libnet-ssleay-perl libodfgen-0.1-1
liborcus-0.17-0 liborcus-parser-0.17-0 libpagemaker-0.0-0 libphobos2-ldc-shared98 libpq5 libraptor2-0 librasqal3
librdf0 libreoffice libreoffice-base libreoffice-base-core libreoffice-base-drivers libreoffice-calc
libreoffice-common libreoffice-core libreoffice-draw libreoffice-gnome libreoffice-gtk3 libreoffice-impress
libreoffice-java-common libreoffice-math libreoffice-nlpsolver libreoffice-report-builder
libreoffice-report-builder-bin libreoffice-script-provider-bsh libreoffice-script-provider-js
libreoffice-script-provider-python libreoffice-sdbc-firebird libreoffice-sdbc-hsqldb libreoffice-sdbc-mysql
libreoffice-sdbc-postgresql libreoffice-style-colibre libreoffice-style-elementary libreoffice-style-yaru
libreoffice-wiki-publisher libreoffice-writer librevenge-0.0-0 libservlet-api-java libservlet3.1-java
libsuitesparseconfig5 libtie-ixhash-perl libtimedate-perl libtommath1 libtry-tiny-perl libuno-cppu3
libuno-cppuhelpergcc3-3 libuno-purpenvhelpergcc3-3 libuno-sal3 libuno-salhelpergcc3-3 libunoloader-java liburi-perl
libvisio-0.1-1 libvte-2.91-0 libvte-2.91-common libvted-3-0 libwebsocket-api-java libwpd-0.10-10 libwpg-0.3-3
libwps-0.4-4 libwww-perl libwww-robotrules-perl libx11-protocol-perl libxft2 libxml-parser-perl libxml-twig-perl
libxml-xpathengine-perl libxmlsec1 libxmlsec1-nss libxv1 libxxf86dga1 libyajl2 lp-solve mysql-common openjdk-11-jre
perl-openssl-defaults python3-uno tilix tilix-common uno-libs-private ure ure-java x11-utils x11-xserver-utils
xdg-utils
0 upgraded, 187 newly installed, 0 to remove and 12 not upgraded.
Need to get 265 MB of archives.
After this operation, 1002 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
sofficeコマンドを使ってExcelをPDF形式に変換する
$ soffice --help
LibreOffice 7.3.7.2 30(Build:2)
Usage: soffice [argument...]
argument - switches, switch parameters and document URIs (filenames).
Using without special arguments:
Opens the start center, if it is used without any arguments.
{file} Tries to open the file (files) in the components
suitable for them.
{file} {macro:///Library.Module.MacroName}
Opens the file and runs specified macros from
the file.
Getting help and information:
--help | -h | -? Shows this help and quits.
--helpwriter Opens built-in or online Help on Writer.
--helpcalc Opens built-in or online Help on Calc.
--helpdraw Opens built-in or online Help on Draw.
--helpimpress Opens built-in or online Help on Impress.
--helpbase Opens built-in or online Help on Base.
--helpbasic Opens built-in or online Help on Basic scripting
language.
--helpmath Opens built-in or online Help on Math.
--version Shows the version and quits.
--nstemporarydirectory
(MacOS X sandbox only) Returns path of the temporary
directory for the current user and exits. Overrides
all other arguments.
General arguments:
--quickstart[=no] Activates[Deactivates] the Quickstarter service.
--nolockcheck Disables check for remote instances using one
installation.
--infilter={filter} Force an input filter type if possible. For example:
--infilter="Calc Office Open XML"
--infilter="Text (encoded):UTF8,LF,,,"
--pidfile={file} Store soffice.bin pid to {file}.
--display {display} Sets the DISPLAY environment variable on UNIX-like
platforms to the value {display} (only supported by a
start script).
User/programmatic interface control:
--nologo Disables the splash screen at program start.
--minimized Starts minimized. The splash screen is not displayed.
--nodefault Starts without displaying anything except the splash
screen (do not display initial window).
--invisible Starts in invisible mode. Neither the start-up logo nor
the initial program window will be visible. Application
can be controlled, and documents and dialogs can be
controlled and opened via the API. Using the parameter,
the process can only be ended using the taskmanager
(Windows) or the kill command (UNIX-like systems). It
cannot be used in conjunction with --quickstart.
--headless Starts in "headless mode" which allows using the
application without GUI. This special mode can be used
when the application is controlled by external clients
via the API.
--norestore Disables restart and file recovery after a system crash.
--safe-mode Starts in a safe mode, i.e. starts temporarily with a
fresh user profile and helps to restore a broken
configuration.
--accept={connect-string} Specifies a UNO connect-string to create a UNO
acceptor through which other programs can connect to
access the API. Note that API access allows execution
of arbitrary commands.
The syntax of the {connect-string} is:
connection-type,params;protocol-name,params
e.g. pipe,name={some name};urp
or socket,host=localhost,port=54321;urp
--unaccept={connect-string} Closes an acceptor that was created with
--accept. Use --unaccept=all to close all acceptors.
--language={lang} Uses specified language, if language is not selected
yet for UI. The lang is a tag of the language in IETF
language tag.
Developer arguments:
--terminate_after_init
Exit after initialization complete (no documents loaded)
--eventtesting Exit after loading documents.
New document creation arguments:
The arguments create an empty document of specified kind. Only one of them may
be used in one command line. If filenames are specified after an argument,
then it tries to open those files in the specified component.
--writer Creates an empty Writer document.
--calc Creates an empty Calc document.
--draw Creates an empty Draw document.
--impress Creates an empty Impress document.
--base Creates a new database.
--global Creates an empty Writer master (global) document.
--math Creates an empty Math document (formula).
--web Creates an empty HTML document.
File open arguments:
The arguments define how following filenames are treated. New treatment begins
after the argument and ends at the next argument. The default treatment is to
open documents for editing, and create new documents from document templates.
-n Treats following files as templates for creation of new
documents.
-o Opens following files for editing, regardless whether
they are templates or not.
--pt {Printername} Prints following files to the printer {Printername},
after which those files are closed. The splash screen
does not appear. If used multiple times, only last
{Printername} is effective for all documents of all
--pt runs. Also, --printer-name argument of
--print-to-file switch interferes with {Printername}.
-p Prints following files to the default printer, after
which those files are closed. The splash screen does
not appear. If the file name contains spaces, then it
must be enclosed in quotation marks.
--view Opens following files in viewer mode (read-only).
--show Opens and starts the following presentation documents
of each immediately. Files are closed after the showing.
Files other than Impress documents are opened in
default mode , regardless of previous mode.
--convert-to OutputFileExtension[:OutputFilterName] \
[--outdir output_dir] [--convert-images-to]
Batch convert files (implies --headless). If --outdir
isn't specified, then current working directory is used
as output_dir. If --convert-images-to is given, its
parameter is taken as the target filter format for *all*
images written to the output format. If --convert-to is
used more than once, the last value of
OutputFileExtension[:OutputFilterName] is effective. If
--outdir is used more than once, only its last value is
effective. For example:
--convert-to pdf *.odt
--convert-to epub *.doc
--convert-to pdf:writer_pdf_Export --outdir /home/user *.doc
--convert-to "html:XHTML Writer File:UTF8" \
--convert-images-to "jpg" *.doc
--convert-to "txt:Text (encoded):UTF8" *.doc
--print-to-file [--printer-name printer_name] [--outdir output_dir]
Batch print files to file. If --outdir is not specified,
then current working directory is used as output_dir.
If --printer-name or --outdir used multiple times, only
last value of each is effective. Also, {Printername} of
--pt switch interferes with --printer-name.
--cat Dump text content of the following files to console
(implies --headless). Cannot be used with --convert-to.
--script-cat Dump text content of any scripts embedded in the files
to console (implies --headless). Cannot be used with
--convert-to.
-env:<VAR>[=<VALUE>] Set a bootstrap variable. For example: to set
a non-default user profile path:
-env:UserInstallation=file:///tmp/test
Ignored switches:
-psn Ignored (MacOS X only).
-Embedding Ignored (COM+ related; Windows only).
--nofirststartwizard Does nothing, accepted only for backward compatibility.
--protector {arg1} {arg2}
Used only in unit tests and should have two arguments.
とりあえず使ってみる。
soffice --headless --convert-to pdf:writer_pdf_Export calendar.xlsx
IPA系フォントのインストール
$ sudo apt-get install fonts-ipaexfont
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
fonts-ipaexfont-gothic fonts-ipaexfont-mincho
The following NEW packages will be installed:
fonts-ipaexfont fonts-ipaexfont-gothic fonts-ipaexfont-mincho
0 upgraded, 3 newly installed, 0 to remove and 12 not upgraded.
Need to get 7954 kB of archives.
After this operation, 14.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu jammy/universe amd64 fonts-ipaexfont-gothic all 00401-3ubuntu1 [3341 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy/universe amd64 fonts-ipaexfont-mincho all 00401-3ubuntu1 [4604 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy/universe amd64 fonts-ipaexfont all 00401-3ubuntu1 [8428 B]
Fetched 7954 kB in 3s (2501 kB/s)
Selecting previously unselected package fonts-ipaexfont-gothic.
(Reading database ... 51724 files and directories currently installed.)
Preparing to unpack .../fonts-ipaexfont-gothic_00401-3ubuntu1_all.deb ...
Unpacking fonts-ipaexfont-gothic (00401-3ubuntu1) ...
Selecting previously unselected package fonts-ipaexfont-mincho.
Preparing to unpack .../fonts-ipaexfont-mincho_00401-3ubuntu1_all.deb ...
Unpacking fonts-ipaexfont-mincho (00401-3ubuntu1) ...
Selecting previously unselected package fonts-ipaexfont.
Preparing to unpack .../fonts-ipaexfont_00401-3ubuntu1_all.deb ...
Unpacking fonts-ipaexfont (00401-3ubuntu1) ...
Setting up fonts-ipaexfont-mincho (00401-3ubuntu1) ...
update-alternatives: using /usr/share/fonts/opentype/ipaexfont-mincho/ipaexm.ttf to provide /usr/share/fonts/truetype/fonts-japanese-mincho.ttf (fonts-japanese-mincho.ttf) in auto mode
Setting up fonts-ipaexfont-gothic (00401-3ubuntu1) ...
update-alternatives: using /usr/share/fonts/opentype/ipaexfont-gothic/ipaexg.ttf to provide /usr/share/fonts/truetype/fonts-japanese-gothic.ttf (fonts-japanese-gothic.ttf) in auto mode
Setting up fonts-ipaexfont (00401-3ubuntu1) ...
Processing triggers for fontconfig (2.13.1-4.2ubuntu5) ..
再度同じコマンドを実行
soffice --headless --convert-to pdf:writer_pdf_Export calendar.xlsx
参考
- LibreOfficeをコマンドラインで使うと便利だよ
https://naruoga.hatenablog.com/entry/2015/12/21/201558 - LibreOffice のインストール(Ubuntu 上)
https://www.kkaneko.jp/tools/ubuntu/libreofficejaubuntu.html - LibreOffice + PHPでExcelからPDF変換しよう!
https://www.wantedly.com/companies/logical-studio/post_articles/309603 - Excel文書のPDF変換で日本語フォントがおかしかった件
https://qiita.com/mingchun_zhao/items/03ed64c1ae3648c00416 - 【LibreOffice】インストール方法
https://zenn.dev/okina/articles/bd471678468771
おわりに
以前、PHP+Laravel構成でPDF変換をできるようなプログラムを書いたので、記憶を遡って soffice
コマンド単体で動かせるように試してみた。
結構簡単にできるので、プログラムでExcel→PDF変換を試したいって人にはおすすめ