Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6644643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:11:27+00:00 2026-05-26T00:11:27+00:00

I am working on a Rails application (Rails 3.1) and have the ability to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:11:28+00:00Added an answer on May 26, 2026 at 12:11 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Rails application where I have users that can add each
I'm working on rails application with postgres db.I have a table called merchant_review_votes where
I'm working on a Rails application where I have some a set of two
I am thinking of working on a Rails application that uses PostgreSQL. I have
Background Currently, I am working on a Rails application. I have different products that
The Rails application I'm working on, has two central pieces: users and groups. A
I'm working on a Rails 3.2 application that will allow users to authenticate with
I have a working Rails application on version 2.3.5 - I am using many
In my rails application I have two models called Kases and Notes. They work
Im working on a Rails based application where I have the need to allow

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.