2011-10-06 52 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,因爲有幾個概念可以理解,但我會一起幫助你,你的功能看起來並不複雜。 –

相關問題