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 8453527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:47:53+00:00 2026-06-10T11:47:53+00:00

I want to add a text on an existing PDF using Rails, so I

  • 0

I want to add a text on an existing PDF using Rails, so I did :

filename = "#{Rails.root}/app/assets/images/sample.pdf"
Prawn::Document.generate("#{Rails.root}/app/assets/images/full_template.pdf", :template => filename) do
  text "Test", :align => :center
end

And when I open full_template.pdf, I have my template PDF + my text “Test”, but this text is written in a bad direction as if my text was written using a mirror.

You can find the two PDF documents here:

Original : http://www.sebfie.com/wp-content/uploads/sample.pdf

Generated : http://www.sebfie.com/wp-content/uploads/full_template.pdf

  • 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-06-10T11:47:54+00:00Added an answer on June 10, 2026 at 11:47 am

    Let’s see… [switching into PDF debugging mode].

    First, I unpack your full_template.pdf with the help of qpdf, a command-line utility “that does structural, content-preserving transformations on PDF files” (self-description):

    qpdf --qdf full_template.pdf qdf---test.pdf
    

    The result, qdf—test.pdf is now more easy to analyse in a normal text editor, because all streams are unpacked.

    Searching for the string “est” finds us this line:

    [(T) 120 (est)] TJ
    

    Poking around a bit more (and looking at qpdf‘s very helpful comments sprinkled into its output!) we find this: the PDF object where your mirrored string “Test” appears in the original PDF is number 22. It is a completely separate object from the rest of the file’s text, and it also is the only one that uses an un-embedded Helvetica font.

    So let’s extract that separately from the original file:

    qpdf --show-object=22 --filtered-stream-data full_template.pdf 
    
     q
     /DeviceRGB cs
     0.000 0.000 0.000 scn
     /DeviceRGB CS
     0.000 0.000 0.000 SCN
     1 w
     0 J
     0 j
     [ ] 0 d
    
     BT
     286.55 797.384 Td
     /F3.0 12 Tf
     [<54> 120 <657374>] TJ
     ET
    
     Q
    

    OK, here the piece [(T) 120 (est)] TJ appears as [<54> 120 <657374>] TJ. We verify this with the help of the ascii command, that prints us a nice ASCII <-> Hex table. That table confirms:

    T  54
    e  65
    s  73
    t  74
    

    What do the other operators mean? We look them up in the official ISO 32000 PDF-1.7 spec, Annex A, “Operator Summary”. Here we find the following bits of info:

     q   : gsave
     Q   : grestore
     cs  : setcolorspace for nonstroking ops
     CS  : setcolorspace for stroking ops
     scn : setcolor for nonstroking ops
     SCN : setcolor for stroking ops
     w   : setlinewidth
     j   : setlinejoin
     J   : setlinecap
     d   : setdash
     BT  : begin text object
     Td  : move text position
     Tf  : set text font and size
     TJ  : show text allowing individual glyph positioning
     Tj  : show text
     ET  : end text object
    

    Nothing suspicious so far…

    However, looking at the other object where the original page content appears in, object number 5, we discover a difference. For example:

    1 0 0 -1 -17.2308 -13.485 Tm
    <0013001c001200130018001200140015> Tj
    

    Here, before each single action of a Tj (show text) the Tm operator (What is this?!?) is in play. Let’s also look up Tm in the PDF spec:

     Tm  : set text matrix and text line matrix
    

    What is strange however, is that this matrix uses 1 0 0 -1 (instead of the more common 1 0 0 1). This leads to the up-side down mirroring of the text.

    Wait a minute!?!

    The original text content is stroked with a mirroring text matrix, but still appears normal?? But your added text doesn’t use any text matrix of its own, but appears mirrored? What is going on?!

    I’m not going to trace it down for more now. My assumption is however, that somewhere in the guts of the original PDF, the authoring software defined an ‘extended graphics state’ which causes all stroking operations to be mirrored by default.

    It seems you’ve done nothing wrong, Sebastien — you’ve just been unlucky with your choice of a test object, and got blessed with a rather weird one. Try it continue your ‘Prawn’ experiments with some other PDFs first…

    One can “fix” your full_template.pdf by replacing this line in qdf—test.pdf:

    286.55 797.384 Td
    

    by this one:

    1 0 0 -1 286.55 797.384 Tm
    

    and then run a last qdf command to fix the (now corrupted by our editing) PDF cross-reference table and stream lenghts:

    qpdf qdf—test.pdf full_template—fixed.pdf

    The console output will show you want it does:

      WARNING: qdf---test.pdf: file is damaged
      WARNING: qdf---test.pdf (file position 151169): xref not found
      WARNING: qdf---test.pdf: Attempting to reconstruct cross-reference table
      WARNING: qdf---test.pdf (object 8 0, file position 9072): attempting to recover stream length
      qpdf: operation succeeded with warnings; resulting file may have some problems
    

    The “fixed” PDF will show the text un-mirrored.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using MenuBar from GWT showcase. I want to add text in the
I have an existing Silverlight 3 app that I want to add Live Mesh
I'm using iTextSharp to load an existing PDF and adding text using the PdfStamper.
I want to add an edit text field to my app when i click
The title sums it all. I want to add a text to an existing
I would like to open an existing pdf, add some text and then output
I want to add text from my database to a UIWebView, but I have
In my view I want to add the text user enter in a textarea
I just want to add some extra text on top of input posted from
I have a System.Windows.Forms.Button control, and I want to add a hover-text explanation of

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.