Writing with StringIo #30

cbron opened this Issue Jan 11, 2012 · 4 comments

2 participants


More of a question than an issue, but if I wanted to make a StringIO with multiple pdfs each with its own background, is there a way to write that out to a pdf ?


Hi @cbron,

I'm not sure I understand your question. Do you want to add a different backgrounds on each page of your pdf ?

Basicly, input or output should be a single pdf (with optionaly several pages, which can be a concatenation of pdfs).

If I my understanding of your question is good, you should use :

@pdftk.background(input_pdf, backgrounds_pdf, :muli => true, :output => output_pdf)
@pdftk.multibackground(input_pdf, backgrounds_pdf, :output => output_pdf)

page 1 of input_pdf will be backgrounded by page 1 of background_pdf
page 2 of input_pdf will be backgrounded by page 2 of background_pdf
ifinput_pdf has more pages than background_pdf, last background will be repeated until end of input_pdf

and of course as usual all ressources (input_pdf, background_pdf, output_pdf) can be String (representing path), File, Tempfile, or StringIO (but one of input_pdf and background_pdf have to be a String).

Tell me if that helped you, or if I misunderstand your question.

See you


Yea thats what I wanted. Or at least that solves my problem. Originally I was wondering if I had generated several pdfs into a string IO was there a way to write it back out:
str = @pdftk.background(input_pdf1, background_pdf1)
str += @pdftk.background(input_pdf2, background_pdf2)

Also you mentioned that with the way you did it above the last background will be repeated until the end of the input_pdf, is there a way to turn that off ? So 5 input_pdf pages, 3 background_pages, and the last 2 input pdf_pages remain untouched ? I could probably burst the full input_pdf and call the normal background method on only the ones that have a background.


Hi Caleb,
What you want to do is to concatenate pdfs. It is possible to do it, but not with your syntax (but could be a good idea to dig in this concept, I'll see it with @tcocca ).

for now you should do it with concatenate

@pdftk.multibackground(input_pdf1, backgrounds_pdf1, :output => output_pdf1)
@pdftk.multibackground(input_pdf2, backgrounds_pdf2, :output => output_pdf2)
@pdftk.concat(output_pdf1, output_pdf2, :output => output_pdf_final)

#or depending on how you'll build your backgrounds.

@pdftk.concat(input_pdf1, input_pdf2, :output => temp_pdf)
@pdftk.multibackground(temp_pdf, backgrounds_pdf, :output => output_pdf)

The backgrounding of different sized pdf cannot be changed, you'll need, to reach desired behaviour, to add white (or transparent) pages at the end of your backgrounds collection and then apply the background. Or in a more cpu consuming way : Apply background, burst last pages, then concatenate untouched pages from original resources.


Yea I ended up writing my code just like this. Thanks for your help man, I love the gem so far.

@cbron cbron closed this Jan 13, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment