Ruby中现有PDF中的水印 [英] Watermark in existing PDF in Ruby
本文介绍了Ruby中现有PDF中的水印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想添加一个动态生成的文本.有没有办法在Ruby中给现有的PDF加水印?
I would like to add a dynamically generated text. Is there a way to watermark an existing PDF in Ruby?
推荐答案
这可以做到:
PDF::Reader
计算文件中的页数.
Prawn
使用输入的pdf的每一页作为模板来创建新的PDF文档.
Prawn
to create a new PDF document using each page of the input pdf as a template.
require 'prawn'
require 'pdf-reader'
input_filename = 'input.pdf'
output_filename = 'output.pdf'
page_count = PDF::Reader.new(input_filename).page_count
Prawn::Document.generate(output_filename, :skip_page_creation => true) do |pdf|
page_count.times do |num|
pdf.start_new_page(:template => input_filename, :template_page => num+1)
pdf.text('WATERMARK')
end
end
但是,在我的测试中,使用最新的Gem版本的Prawn(0.12),输出文件的大小很大,但是将我的Gemfile指向github上的master分支后,一切正常.
However, in my testing the output file size was huge with the latest Gem version of Prawn (0.12), but after pointing my Gemfile at the master branch on github, all worked fine.
这篇关于Ruby中现有PDF中的水印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文