

我在处理pdf文件时经常遇到一些问题. 每次我绞尽脑汁将这些pdf文件打到底时pdflib设置字体,都会使用1000种pdf文件和一千种处理方法.
我又是地鼠,因此本文将使用goper透视图枚举我经历过的各种pdf处理场景,例如:
pdf渲染
pdf校验
pdf加水印
pdf获取页数
pdf合并
pdf拆分
修复受损pdf
pdf转png
识别pdf中的字体
pdf解密
...
本文主要是场景问题的列表. 您可以根据标题提取感兴趣的部分.
我在许多pdf问题上都不是很. 如有任何疑问,请与我联系.
基于html页面渲染pdf,我使用了以下两种方案:
wkhtmltopdf是基于Qt WebKit呈现引擎实现的用于将HTML页面呈现为PDF的命令行工具
易于使用:
## 将一个静态html页面打印成pdf
$ wkhtmltopdf input.html output.pdf
## 将一个网页打印成pdf
$ wkhtmltopdf https://www.google.com output.pdf
wkhtmltopdf具有很多参数,例如:
支持发送http发布请求,适合将定制开发的网页呈现为pdf文件:
$ wkhtmltopdf --help
...
--post <name> <value> Add an additional post field (repeatable)
...
支持javascript脚本,在渲染pdf之前先修改html:
$ wkhtmltopdf --run-script "javascript:(function(){document.getElementsByClassName('dom_class_name')[0].style.display = 'none'}())" page input.html output.pdf
有关更多详细参数,请参阅官方网站文档
如果您使用Go,则有一个第三方软件包封装了wkhtmltopdf的用法: go-wkhtmltopdf
chromedp是用于驱动浏览器的软件包,该浏览器以更快,更轻松的方式在Go中支持Chrome DevTools协议,而无需外部依赖(例如Selenium或PhantomJS).
使用方法:

package main
import (
"context"
"io/ioutil"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
"errors"
)
func main(){
err := ChromedpPrintPdf("https://www.google.com", "/path/to/file.pdf")
if err != nil {
fmt.Println(err)
return
}
}
func ChromedpPrintPdf(url string, to string) error {
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
var buf []byte
err := chromedp.Run(ctx, chromedp.Tasks{
chromedp.Navigate(url),
chromedp.WaitReady("body"),
chromedp.ActionFunc(func(ctx context.Context) error {
var err error
buf, _, err = page.PrintToPDF().
Do(ctx)
return err
}),
})
if err != nil {
return fmt.Errorf("chromedp Run failed,err:%+v", err)
}
if err := ioutil.WriteFile(to, buf, 0644); err != nil {
return fmt.Errorf("write to file failed,err:%+v", err)
}
return nil
}
我知道支持pdf水印的工具是:
在unidoc平台上开发的
unipdf是用Go语言编写的PDF库. 它提供API和CLI使用模式,并支持以下功能:
$ unipdf -h
...
Available Commands:
decrypt Decrypt PDF files
encrypt Encrypt PDF files
explode Explodes the input file into separate single page PDF files
extract Extract PDF resources
form PDF form operations
grayscale Convert PDF to grayscale
help Help about any command
info Output PDF information
merge Merge PDF files
optimize Optimize PDF files
passwd Change PDF passwords
rotate Rotate PDF file pages
search Search text in PDF files
split Split PDF files
version Output version information and exit
watermark Add watermark to PDF files
...
在CLI模式下添加水印
$ unipdf watermark in.pdf watermark.png -o out.pdf
Watermark successfully applied to in.pdf
Output file saved to out.pdf
使用API添加水印,您可以直接参考unipdf github示例
注意: Unidoc产品需要购买许可证才能使用
pdfcpu是用Go编写的PDF处理库,提供了API和CLI模式的使用.
支持以下功能:
$ pdfcpu help
...
The commands are:
attachments list, add, remove, extract embedded file attachments
changeopw change owner password
changeupw change user password
decrypt remove password protection
encrypt set password protection
extract extract images, fonts, content, pages, metadata
fonts install, list supported fonts
grid rearrange pages or images for enhanced browsing experience
import import/convert images to PDF
info print file info
merge concatenate 2 or more PDFs
nup rearrange pages or images for reduced number of pages
optimize optimize PDF by getting rid of redundant page resources
pages insert, remove selected pages
paper print list of supported paper sizes
permissions list, set user access permissions
rotate rotate pages
split split multi-page PDF into several PDFs according to split span
stamp add, remove, update text, image or PDF stamps for selected pages
trim create trimmed version of selected pages
validate validate PDF against PDF 32000-1:2008 (PDF 1.7)
version print version
watermark add, remove, update text, image or PDF watermarks for selected pages
...
使用CLI工具将水印添加为图片:
$ pdfcpu watermark add -mode image 'voucher_watermark.png' 's:1 abs, rot:0' in.pdf out.pdf
调用api添加水印
package main
import (
"github.com/pdfcpu/pdfcpu/pkg/api"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
)
func main() {
onTop := false
wm, _ := pdfcpu.ParseImageWatermarkDetails("watermark.png", "s:1 abs, rot:0", onTop)
api.AddWatermarksFile("in.pdf", "out.pdf", nil, wm, nil)
}
cpdf是具有功能丰富的开源免费PDF命令行工具库,例如:
合并pdf:
$ cpdf -merge input1.pdf input2.pdf -o output.pdf
$ unipdf merge output.pdf input1.pdf input2.pdf

使用API合并pdf,请参考unpdf github示例
$ pdfcpu merge output.pdf input1.pdf input2.pdf
注意: pdfcpu仅支持版本低于PDF V1.7的pdf文件
## 逐页拆分成单个pdf
$ cpdf -split in.pdf 1 even -chunk 1 -o ./out%%%.pdf
## 将第一页拆分出来
$ unipdf split input.pdf out.pdf 1-1
使用api分割pdf,请参考unipdf github示例
$ pdfcpu split in.pdf .
MuPDF是轻型PDF,XPS和电子书查看器.
MuPDF由一个软件库,命令行工具和各种平台的查看器组成.
下载mupdf以获取一些工具,例如:
mupdf
pdfdraw
pdfinfo
pdfclean
pdfextract
pdfshow
xpsdraw
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-160902-1.html
这就是报应