LoginSignup
8
2

More than 3 years have passed since last update.

go言語でPDFファイルの分割【pdfcpu】

Posted at

はじめに

深い理由はないのですが、PDFファイルの分割をローカルでしたくなったため書きました。
以下のライブラリを使用させていただいてます。

非常に多くのPDF操作が出来そうですが、
今回は分割のみの紹介です。

免責など

・本記事上のコマンド、コードについて使用したことによる問題については一切責任を負いません。

go get

go get github.com/hhrutter/pdfcpu/cmd/...

コード

test.go
package main

import (
    "github.com/hhrutter/pdfcpu/pkg/pdfcpu"
    "github.com/hhrutter/pdfcpu/pkg/api"
    "flag"
)
func main() {
    var pdfname = flag.String("n","test.pdf","file name")
    var dirname = flag.String("d","output","directory name")
    var splitnum = flag.Int("s",1,"split span")

    flag.Parse()
    config := pdfcpu.NewDefaultConfiguration()
    _, err := api.Process(api.SplitCommand(*pdfname, *dirname, *splitnum, config))
    if err != nil {
        return
    }
}

api.SplitCommandは第一引数に処理するPDFファイル名、第二引数に出力先フォルダ名、
第三引数に分割するPDFのページ間隔を指定します。(1なら1ページずつ、2なら2ページずつ)

実行

go run test.go -n a.pdf -d output_dir -s 1

outputフォルダに、
a_1.pdf、a_2.pdf...
とページずつ出力されます。

第三引数に2を設定すると
a_1-2.pdf、a_3-4.pdf...
と出力されます。

終わりに

このライブラリですが執筆時点(2019年5月13日)でアルファ版とのことですので、
ご利用を検討されている方はご留意ください。

8
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
2