在 Rails 应用程序中更改上传的 PDF 文件

发布于 2024-12-08 09:42:32 字数 414 浏览 0 评论 0 原文

我正在开发 Rails 应用程序 (Rails 3.1),并且能够上传 PDF 文件。其他用户登录后就可以下载 PDF 文件。

我真正想做的是将当前登录用户的用户名附加到每个 PDF 页面的底部。因此,PDF 中每个页面的底部都是这样的:

Downloaded from ww.mysite.com by Mr U. Name

我知道如何使 Rails 从视图中输出 pdf(根据一些教程),但我从未需要更改已上传的 PDF。

旁注:PDF 文件将从 Powerpoint 和/或 Keynote 输出 - 因此每个页面都是一张“幻灯片”。

I am working on a Rails application (Rails 3.1) and have the ability to upload a PDF file. The PDF file can then be downloaded by other users once they have logged in.

What I would really like to do is append the username of the current logged in user to the bottom of every one of the PDF pages. So at the bottom of every page in the PDF would be something like:

Downloaded from ww.mysite.com by Mr U. Name

I know how to make rails output a pdf from a view (as per a few tutorials), but I have never had to alter a PDF that has already been uploaded.

Side note: the PDF files are going to be outputs from Powerpoint and/or Keynote - so each page is a 'slide'.

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

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

发布评论

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

评论(1

欢你一世 2024-12-15 09:42:32

纯 ruby​​ 解决方案是不存在的,但我一直在使用 python 脚本进行相同类型的操作(将打印标记添加到现有 PDF),使用 pyPdfpycairo

您可以在 Github 上找到它,大部分代码位于 draw-outline.pyoutline.py

如果您对此解决方案感兴趣,我可以提取一个快速教程并将其发布在这里。

更新

我提取了一个快速脚本,可以将文本添加到任意 PDF 的每一页,它既快速又脏,但应该可以完成工作。

用法:

Usage: name-generator.py [options] filename

Options:
  -h, --help            show this help message and exit
  -o OUTPUT_FILENAME, --output=OUTPUT_FILENAME
                        output filename, defaults to output.pdf
  -t TEXT, --text=TEXT  text to add
  -s SIZE, --size=SIZE  size of the text, in millimeters
  -x X                  horizontal position of the text in millimeters, the
                        origin is the left side
  -y Y                  vertical position of the text in millimeters, the
                        origin is the top side

代码:

#!/usr/bin/env python
from optparse import OptionParser
import os.path

# Constants
MM_TO_PT = 2.834645669
DEFAULT_OUTPUT = "output.pdf"

# Option parsing
parser = OptionParser("Usage: %prog [options] filename")
parser.add_option("-o", "--output", type="string", dest="output_filename", default=DEFAULT_OUTPUT, help="output filename, defaults to %s" % DEFAULT_OUTPUT)
parser.add_option("-t", "--text", type="string", dest="text", help="text to add")
parser.add_option("-s", "--size", type="float", dest="size", default=10, help="size of the text, in millimeters")
parser.add_option("-x", type="int", dest="x", default=100, help="horizontal position of the text in millimeters, the origin is the left side")
parser.add_option("-y", type="int", dest="y", default=100, help="vertical position of the text in millimeters, the origin is the top side")

(options, args) = parser.parse_args()

if len(args) != 1 or not os.path.isfile(args[0]):
  parser.print_help()
  exit()


# Pdf manipulation class
import cairo
import StringIO

class TextCreator:

  def __init__(self, width, height):
    self.width, self.height = width, height

    self.output = StringIO.StringIO()
    self.surface = cairo.PDFSurface(
        self.output,
        self.width * MM_TO_PT,
        self.height * MM_TO_PT
    )

  def print_text(self, text, size, x, y):
    context = self.create_context()

    context.move_to(x, y)

    context.set_font_size(size)
    context.show_text(text)

    self.surface.flush()
    self.surface.finish()

    return self.output

  def create_context(self):
    context = cairo.Context(self.surface)
    context.set_source_rgb(0, 0, 0)
    context.set_line_width(0.2)

    # Allow drawing on the context using human-friendly units (millimeters)
    context.scale(MM_TO_PT, MM_TO_PT)

    return context


# Let's get to work
from pyPdf import PdfFileWriter, PdfFileReader

# Read the input and prepare the output document
filename = args[0]
document = PdfFileReader(file(filename, "rb"))
output = PdfFileWriter()

for page_num in range(document.getNumPages()):
  # Get the page dimensions
  page = document.getPage(page_num)
  box = page.mediaBox
  # PDF dimensions are in points, convert them to millimeters
  width = round(float(box[2]) / MM_TO_PT)
  height = round(float(box[3]) / MM_TO_PT)

  # Create a PDF page containing the text
  text_creator = TextCreator(
      width,
      height
  )
  text = text_creator.print_text(options.text, options.size, options.x, options.y)

  # Merge the text with the current page and add it to the output
  page.mergePage(PdfFileReader(text).getPage(0))
  output.addPage(page)


outputStream = file(options.output_filename, "wb")
output.write(outputStream)
outputStream.close()

The pure ruby solutions are non-existent, but I've been doing the same kind of manipulation (adding print marks to an existing PDF) with a python script, using pyPdf and pycairo.

You can find it on Github, the bulk of the code is in draw-outline.py and outline.py.

I can extract a quick tutorial and post it here if you're interested by this solution.

Update

I've extracted a quick script to add text to every page of an arbitrary PDF, it's quick and dirty but it should do the job.

Usage:

Usage: name-generator.py [options] filename

Options:
  -h, --help            show this help message and exit
  -o OUTPUT_FILENAME, --output=OUTPUT_FILENAME
                        output filename, defaults to output.pdf
  -t TEXT, --text=TEXT  text to add
  -s SIZE, --size=SIZE  size of the text, in millimeters
  -x X                  horizontal position of the text in millimeters, the
                        origin is the left side
  -y Y                  vertical position of the text in millimeters, the
                        origin is the top side

Code:

#!/usr/bin/env python
from optparse import OptionParser
import os.path

# Constants
MM_TO_PT = 2.834645669
DEFAULT_OUTPUT = "output.pdf"

# Option parsing
parser = OptionParser("Usage: %prog [options] filename")
parser.add_option("-o", "--output", type="string", dest="output_filename", default=DEFAULT_OUTPUT, help="output filename, defaults to %s" % DEFAULT_OUTPUT)
parser.add_option("-t", "--text", type="string", dest="text", help="text to add")
parser.add_option("-s", "--size", type="float", dest="size", default=10, help="size of the text, in millimeters")
parser.add_option("-x", type="int", dest="x", default=100, help="horizontal position of the text in millimeters, the origin is the left side")
parser.add_option("-y", type="int", dest="y", default=100, help="vertical position of the text in millimeters, the origin is the top side")

(options, args) = parser.parse_args()

if len(args) != 1 or not os.path.isfile(args[0]):
  parser.print_help()
  exit()


# Pdf manipulation class
import cairo
import StringIO

class TextCreator:

  def __init__(self, width, height):
    self.width, self.height = width, height

    self.output = StringIO.StringIO()
    self.surface = cairo.PDFSurface(
        self.output,
        self.width * MM_TO_PT,
        self.height * MM_TO_PT
    )

  def print_text(self, text, size, x, y):
    context = self.create_context()

    context.move_to(x, y)

    context.set_font_size(size)
    context.show_text(text)

    self.surface.flush()
    self.surface.finish()

    return self.output

  def create_context(self):
    context = cairo.Context(self.surface)
    context.set_source_rgb(0, 0, 0)
    context.set_line_width(0.2)

    # Allow drawing on the context using human-friendly units (millimeters)
    context.scale(MM_TO_PT, MM_TO_PT)

    return context


# Let's get to work
from pyPdf import PdfFileWriter, PdfFileReader

# Read the input and prepare the output document
filename = args[0]
document = PdfFileReader(file(filename, "rb"))
output = PdfFileWriter()

for page_num in range(document.getNumPages()):
  # Get the page dimensions
  page = document.getPage(page_num)
  box = page.mediaBox
  # PDF dimensions are in points, convert them to millimeters
  width = round(float(box[2]) / MM_TO_PT)
  height = round(float(box[3]) / MM_TO_PT)

  # Create a PDF page containing the text
  text_creator = TextCreator(
      width,
      height
  )
  text = text_creator.print_text(options.text, options.size, options.x, options.y)

  # Merge the text with the current page and add it to the output
  page.mergePage(PdfFileReader(text).getPage(0))
  output.addPage(page)


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