我正在开发Rails应用程序(Rails 3.1),并且能够上传PDF文件。其他用户登录后可以下载PDF文件。
我真正想做的是将当前登录用户的用户名附加到每个PDF页面的底部。所以在PDF的每个页面的底部都会是这样的:
Downloaded from ww.mysite.com by Mr U. Name
我知道如何从视图中输出一个pdf(根据一些tutorials),但我从来没有必要更改已经上传的PDF。
附注:PDF文件将是Powerpoint和/或Keynote的输出 - 因此每个页面都是“幻灯片”。
答案 0 :(得分:2)
纯ruby解决方案不存在,但我使用pyPdf和pycairo使用python脚本进行相同类型的操作(向现有PDF添加打印标记)。
您可find it on Github,大部分代码位于draw-outline.py和outline.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()