Ruby中现有PDF中的水印

我想添加一个动态生成的文本。 有没有办法在Ruby中为现有PDF添加水印?

这样做:

PDF::Reader来计算文件中的页数。

Prawn使用输入pdf的每个页面作为模板创建一个新的PDF文档。

 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上的主分支后,一切正常。

另一种选择是使用PDFTK。 它可用于添加水印并创建新PDF。 也许大虾会用它的模板做同样的事情。

pdftk in.pdf background arecibo.pdf output wmark1.pdf

更多信息: http : //rhodesmill.org/brandon/2009/pdf-watermark-margins/

有一个名为active_pdftk的ruby包装器gem支持后台处理,因此您不必自己执行shell命令。

Prawn不再支持模板了……

试试combine_pdf gem 。

您可以组合,水印,页码并将简单文本添加到现有PDF文件(包括创建没有PDF链接的简单目录)。

它是一个非常简单和基本的PDF库,用纯Ruby编写,没有依赖关系。

这个例子可以适合你的情况(它来自自述文件):

 # load the logo as a pdf page company_logo = CombinePDF.load("company_logo.pdf").pages[0] # load the content file pdf = CombinePDF.load "content_file.pdf" # inject the logo to each page in the content pdf.pages.each {|page| page << company_logo} # save to a new file, with the logo. pdf.save "content_with_logo.pdf" 

查看Prawn( http://github.com/sandal/prawn )获取ruby和Prawnto( http://github.com/thorny-sun/prawnto )获取Ruby on Rails。

您可能想要使用图像嵌入function或背景图像function。

以下是使用背景图像的示例http://cracklabs.com/prawnto/code/prawn_demos/source/general/background

使用Ruport 。

Googling ruby pdf watermark第一个结果。