# PrintOperation can't change it's page\_setup

**URL:** <https://www.ruby-forum.com/t/printoperation-cant-change-its-page-setup/118914>\
**Category:** Ruby-Gnome 2\
**Created:** [November 1, 2007, 10:57am UTC](https://www.ruby-forum.com/t/printoperation-cant-change-its-page-setup/118914 "2007-11-01T10:57:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [November 1, 2007, 10:57am UTC](https://www.ruby-forum.com/t/printoperation-cant-change-its-page-setup/118914/1 "2007-11-01T10:57:37Z")

</div>

hi,

i wanted to eliminate the overhead on the user, who first have to set  
the page\_setup with Gtk::PrintOperation.run\_page\_setup\_dialog, and then  
print from the other dialog  
Gtk::PrintOperation.run(Gtk::PrintOperation::Action::PRINT\_DIALOG).

so i used the| |create-custom-widget and custom-widget-apply signals to  
do that, because i found no other method for it.  
first i created the page\_size combo and the orientation combo in it, and  
found out that changing the values messes up the cairo context, so i  
wrote an example script for this demo, which calls the original  
run\_page\_setup\_dialog (for simplicity).  
it’s attached.

the usage:

1. run it without changing the page\_setup in the custom tab, print to  
file.
2. run it and change the page\_setup, and print to file.
3. compare the pdfs.

i also attach examples i created.

creating and positioning pango layouts works fine. (why)  
the cairo\_context looses it’s 0,0 coordinates?  
i checked margins, they looked fine to me (page\_setup selected from the  
combo brings us the default margins for that format).  
if i change the page\_setup before i call the PrintOperation.run, than  
it’s good.

please if anyone have any clue drop me an answer.  
thanks for reading.

balint

---

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [November 5, 2007, 6:55pm UTC](https://www.ruby-forum.com/t/printoperation-cant-change-its-page-setup/118914/2 "2007-11-05T18:55:34Z")

</div>

Kou - since you’re the cairo expert, can you please take a look at this  
issue, when you have the time.  
is there a way i could reinitialize the cairo context (or should i?)  
after changing the page\_setup?

thanks in advice

---

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [November 6, 2007, 2:19pm UTC](https://www.ruby-forum.com/t/printoperation-cant-change-its-page-setup/118914/3 "2007-11-06T14:19:17Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“Re: [ruby-gnome2-devel-en] PrintOperation can’t change it’s  
page\_setup” on Mon, 05 Nov 2007 19:53:37 +0200,  
Dobai-Pataky Bálint [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Kou - since you’re the cairo expert, can you please take a look at this  
> issue, when you have the time.  
> is there a way i could reinitialize the cairo context (or should i?)  
> after changing the page\_setup?

You can’t.  
Use gtk/sample/misc/print.rb style approach. If you want to  
know details, see  
gtk/gtkprintoperation-unix.c:handle\_print\_response() in GTK+  
source.

## Thanks,

kou

---

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [November 7, 2007, 9:15am UTC](https://www.ruby-forum.com/t/printoperation-cant-change-its-page-setup/118914/4 "2007-11-07T09:15:46Z")

</div>

Mathieu B. wrote:

> Dobai-Pataky BÃ¡lint wrote:
> 
> > > i wanted to eliminate the overhead on the user, who first have to set  
> > > the page\_setup with Gtk::PrintOperation.run\_page\_setup\_dialog, and  
> > > then print from the other dialog  
> > > Gtk::PrintOperation.run(Gtk::PrintOperation::Action::PRINT\_DIALOG).
> 
> Why do you want to change the cairo context for doing that?

because i think that the cairo part of the context gets messed up. (see  
the previously attached simple code and the resulting pdfs).

> In Fantasdic, I have a class Print that extends Gtk::PrintOperation. In  
> initialize I set the default settings like this:

yeah, but you have your app the page\_size set up, and you have it  
available on your Print::initialize.  
try to put some page\_setup related settings in the PrintOperation.run’s  
tabs (or change the page\_setup after the PrintOperation.run was invoked)  
if you print only PangoLayouts than it’ll work, if you draw to the Cairo  
context that’ll mess up coordinates.

> # with this option disabled, (0,0) is the the upper left corner
> 
> # taking into account margins !
> 
> self.use\_full\_page = false  
> self.unit = Gtk::PaperSize::UNIT\_POINTS
> 
> # set default paper size
> 
> page\_setup = Gtk::PageSetup.new  
> paper\_size = Gtk::PaperSize.new(Gtk::PaperSize.default)  
> page\_setup.paper\_size\_and\_default\_margins = paper\_size  
> self.default\_page\_setup = page\_setup

i tried this approach for setting the page\_setup, but this works well  
only in initialize of the PrintOperation.  
i know you played around with Gtk::PrintOperaion a lot, until you wrote  
the fantasdic print objects.  
would you please take a look at the source code attached in my first  
mail - it’s simple,  
would you please explain it to me your understanding about it printing  
A5 portrait?

---

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [November 6, 2007, 9:46pm UTC](https://www.ruby-forum.com/t/printoperation-cant-change-its-page-setup/118914/5 "2007-11-06T21:46:34Z")

</div>

Dobai-Pataky BÃ¡lint wrote:

> > i wanted to eliminate the overhead on the user, who first have to set  
> > the page\_setup with Gtk::PrintOperation.run\_page\_setup\_dialog, and  
> > then print from the other dialog  
> > Gtk::PrintOperation.run(Gtk::PrintOperation::Action::PRINT\_DIALOG).  
> > Why do you want to change the cairo context for doing that?

In Fantasdic, I have a class Print that extends Gtk::PrintOperation. In  
initialize I set the default settings like this:

# with this option disabled, (0,0) is the the upper left corner

# taking into account margins !

self.use\_full\_page = false  
self.unit = Gtk::PaperSize::UNIT\_POINTS

# set default paper size

page\_setup = Gtk::PageSetup.new  
paper\_size = Gtk::PaperSize.new(Gtk::PaperSize.default)  
page\_setup.paper\_size\_and\_default\_margins = paper\_size  
self.default\_page\_setup = page\_setup

For more details:

[http://svn.gnome.org/viewvc/fantasdic/trunk/lib/fantasdic/ui/print.rb?revision=218&view=markup](http://svn.gnome.org/viewvc/fantasdic/trunk/lib/fantasdic/ui/print.rb?revision=218&view=markup)

In my class MainApp, I have methods to load/save the settings from/to  
the app config file. Also, when the user clicks on “print”, if a  
@page\_setup variable exists (either set from the page setup dialog or  
from the config file), I pass it to my Print object. Otherwise, it’s no  
problem since default values have been set as explained above.

For more details:

[http://svn.gnome.org/viewvc/fantasdic/trunk/lib/fantasdic/ui/main\_app.rb?revision=269&view=markup](http://svn.gnome.org/viewvc/fantasdic/trunk/lib/fantasdic/ui/main_app.rb?revision=269&view=markup)

In particular, see:

- load\_print\_preferences
- save\_print\_preferences
- on\_print\_setup
- on\_print
- on\_print\_preview

HTH,  
Mathieu B.
