开发者

Alter an uploaded PDF file within a Rails application

开发者 https://www.devze.com 2023-04-11 18:26 出处:网络
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.

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'.


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()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号