2011-10-06 29 views
3

我正在使用Rails应用程序(Rails 3.1)并且能够上传PDF文件。然后,PDF文件可以在其他用户登录后下载。在Rails应用程序中更改上传的PDF文件

我真正想要做的是将当前登录用户的用户名添加到每个PDF页面的底部。所以在PDF的每个页面的底部会是这样的:

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

我知道如何使轨输出从查看PDF(按几tutorials),但我从来没有改变已上传的PDF。

备注:PDF文件将成为Powerpoint和/或Keynote的输出,因此每个页面都是一个“幻灯片”。

回答

2

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

您可以find it on 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() 
+0

我从来没有碰到Python的 - 我绝对没有和红宝石结合吧!是复杂的吗? – dannymcc

+0

如果您没有使用高级Python特性,并且您已经在Ruby中掌握了面向对象的编程,那么您将会相当快地完成它。痛苦点将是pycairo,因为有几个概念可以理解,但我会一起帮助你,你的功能看起来并不复杂。 –

相关问题