0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[pdfcpu] pdf-cpuでbinaryからpdfをうまく読み込めない

Posted at

サマリ

  • pdf-cpuでctxを生成するときはOptimizeを実行することを忘れないようにすること。
  • OKな例: ReadValidateAndOptimize を呼び出す。
  • NGな例: ReadContext だけ呼び出す。エラーは返ってこないが、ctx.PageCountなどを実行するとエラーになる。

内容

  • golangでpdf操作ができるライブラリ。一通りの機能は備わっている。
  • interfaceが2つある
    1. CommandFiles: 入出力pathを受け取り、入力pathのファイルを読み取り、出力pathにファイル生成するタイプ
    2. Command: reader, writerを受け取り、readerからwriterに書き出すタイプ
  • 今回対象するのは2つ目。
  • 2つ目の場合入力はreaderを渡すタイプとctxを渡すタイプがあり、それぞれ利用したいAPIで異なる。
    • 例1: extractPage: page切り取りAPI。contextを渡す。
    • 例2: crop: pageを切り取るAPI。
  • Contextを生成できるAPIは3種類(methods名末尾にFilesがついているものはpathを受け取るものであるため、省略)
  • ReadContextReadAndValidate を利用すると、うまくPDFを読み取ることができない。(以下コード例)
ctx, err := api.ReadValidateAndOptimize(reader2, model.NewDefaultConfiguration())
if err != nil {
    return nil, errors.Wrap(err, "model.NewContext") // errorはraiseされない
}
fmt.Println(ctx.PageCount) // page数に関わらず0が出力される。
  • よって ReadValidateAndOptimize を利用するように気をつけること。
    • NewOptimize を別途呼び出して対応することも可能
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?