Ruby 中现有 PDF 中的水印

发布于 2024-08-30 00:02:11 字数 46 浏览 6 评论 0原文

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

I would like to add a dynamically generated text. Is there a way to watermark an existing PDF in Ruby?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

和我恋爱吧 2024-09-06 00:02:11

这将执行以下操作:

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

This will do it:

PDF::Reader to count the number of pages in the file.

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

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.

上课铃就是安魂曲 2024-09-06 00:02:11

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

pdftk in.pdf 背景 arecibo.pdf 输出 wmark1.pdf

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

有一个名为 active_pdftk 支持后台,因此您不必自己执行 shell 命令。

Another option is to use PDFTK. It can be used to add a watermark and create a new PDF. Maybe prawn will do the same thing with it's templating.

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

Some more info: http://rhodesmill.org/brandon/2009/pdf-watermark-margins/

There is a ruby wrapper gem called active_pdftk which supports backgrounding, so you don't have to do the shell commands yourself.

戴着白色围巾的女孩 2024-09-06 00:02:11

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 doesn't support templates anymore...

Try the combine_pdf gem.

You can combine, watermark, page-number and add simple text to existing PDF files (including the creation of a simple table of contents without PDF links).

It's a very simple and basic PDF library, written in pure Ruby with no dependencies.

This example can fit your situation (it's from the readme file):

# 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"
决绝 2024-09-06 00:02:11

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

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

这是使用背景图像的示例 http://cracklabs.com/prawnto/代码/prawn_demos/source/general/background

Check out Prawn(http://github.com/sandal/prawn) for just ruby and Prawnto(http://github.com/thorny-sun/prawnto) for Ruby on Rails.

You are probably going to want to either use the image embedding functionality or the background image functionality.

Here's an example of using a background image http://cracklabs.com/prawnto/code/prawn_demos/source/general/background

洒一地阳光 2024-09-06 00:02:11

使用 Ruport

谷歌搜索ruby pdf水印的第一个结果。

Use Ruport.

1st result for Googling ruby pdf watermark.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文