Index: src/gtkhtml-types.h
===================================================================
--- src/gtkhtml-types.h	(revision 8475)
+++ src/gtkhtml-types.h	(working copy)
@@ -52,6 +52,17 @@ typedef gboolean (* GtkHTMLSaveReceiverF
 					      size_t       len,
 					      gpointer     user_data);
 
+typedef gint    (*GtkHTMLPrintCalcHeight) (GtkHTML *html,
+					   GtkPrintOperation *operation,
+                                           GtkPrintContext *context,
+                                           gpointer user_data);
+typedef void    (*GtkHTMLPrintDrawFunc)   (GtkHTML *html,
+					   GtkPrintOperation *operation,
+					   GtkPrintContext *context,
+					   gint page_nr,
+					   PangoRectangle *rec,
+					   gpointer user_data);
+
 typedef void (*GtkHTMLPrintCallback) (GtkHTML *html, GtkPrintContext *print_context,
 				      gdouble x, gdouble y, gdouble width, gdouble height, gpointer user_data);
 
Index: src/htmlengine-print.c
===================================================================
--- src/htmlengine-print.c	(revision 8475)
+++ src/htmlengine-print.c	(working copy)
@@ -22,7 +22,6 @@
 #include <config.h>
 #include <gtk/gtk.h>
 #include <glib/gi18n-lib.h>
-#include <libgnomeui/gnome-dialog-util.h>
 #include "gtkhtml.h"
 #include "gtkhtmldebug.h"
 #include "gtkhtml-private.h"
@@ -36,23 +35,33 @@
 /* #define CLIP_DEBUG */
 
 static void
-print_header_footer (HTMLPainter *painter, HTMLEngine *engine, gint width, gint y, gdouble height,
-		     GtkHTMLPrintCallback cb, gpointer user_data)
+print_header_footer (HTMLPainter *painter,
+		     HTMLEngine *engine,
+		     gint width,
+		     gint y,
+		     gdouble height,
+		     GtkHTMLPrintCallback callback,
+		     gpointer user_data)
 {
 	HTMLPrinter *printer = HTML_PRINTER (painter);
 	GtkPrintContext *context = printer->context;
-	gdouble gx, gy;
 	cairo_t *cr;
 	
 	cr = gtk_print_context_get_cairo_context (context);
 
 	cairo_save (cr);
-	html_painter_set_clip_rectangle (painter, 0, y, width, SCALE_GNOME_PRINT_TO_ENGINE (height));
+	html_painter_set_clip_rectangle (
+		painter, 0, y, width, SCALE_GNOME_PRINT_TO_ENGINE (height));
 #ifdef CLIP_DEBUG
-	html_painter_draw_rect (painter, 0, y, width, SCALE_GNOME_PRINT_TO_ENGINE (height));
+	html_painter_draw_rect (
+		painter, 0, y, width, SCALE_GNOME_PRINT_TO_ENGINE (height));
 #endif
-	html_printer_coordinates_to_gnome_print (printer, 0, y, &gx, &gy);
-	(*cb) (GTK_HTML (engine->widget), context, gx, gy, SCALE_ENGINE_TO_GNOME_PRINT (width), height, user_data);
+	(*callback) (
+		GTK_HTML (engine->widget), context,
+		SCALE_ENGINE_TO_GNOME_PRINT (0),
+		SCALE_ENGINE_TO_GNOME_PRINT (y),
+		SCALE_ENGINE_TO_GNOME_PRINT (width),
+		height, user_data);
 	cairo_restore (cr);
 }
 
@@ -60,177 +69,442 @@ static void
 print_page (HTMLPainter *painter,
 	    HTMLEngine *engine,
 	    gint start_y,
-	    gint page_width, gint page_height, gint body_height,
-	    gdouble header_height, gdouble footer_height,
-	    GtkHTMLPrintCallback header_print, GtkHTMLPrintCallback footer_print, gpointer user_data)
+	    gint page_width,
+	    gint page_height,
+	    gint body_height,
+	    gdouble header_height,
+	    gdouble footer_height,
+	    GtkHTMLPrintCallback header_print,
+	    GtkHTMLPrintCallback footer_print,
+	    gpointer user_data)
 {
 	HTMLPrinter *printer = HTML_PRINTER (painter);
 	GtkPrintContext *context = printer->context;
 	cairo_t *cr;
 
 	cr = gtk_print_context_get_cairo_context (context);
+
 	html_painter_begin (painter, 0, 0, page_width, page_height);
-	if (header_print)
-		print_header_footer (painter, engine, page_width, 0, header_height, header_print, user_data);
+
+	if (header_print != NULL)
+		print_header_footer (
+			painter, engine, page_width, 0, header_height,
+			header_print, user_data);
 
 	cairo_save (cr);
-	html_painter_set_clip_rectangle (painter, 0, header_height, page_width, body_height);
+	html_painter_set_clip_rectangle (
+		painter, 0, header_height, page_width, body_height);
 #ifdef CLIP_DEBUG
-	html_painter_draw_rect (painter, 0, header_height, page_width, body_height);
+	html_painter_draw_rect (
+		painter, 0, header_height, page_width, body_height);
 #endif
-	html_object_draw (engine->clue, painter,
-			  0, start_y,
-			  page_width, body_height,
-			  0, -start_y + header_height);  
+	html_object_draw (
+		engine->clue, painter, 0, start_y, page_width,
+		body_height, 0, -start_y + header_height);  
 	cairo_restore (cr);
-	if (footer_print)
-		print_header_footer (painter, engine, page_width, page_height - 
-				SCALE_GNOME_PRINT_TO_ENGINE (footer_height), footer_height, footer_print, user_data); 	
+
+	if (footer_print != NULL)
+		print_header_footer (
+			painter, engine, page_width, page_height - 
+			SCALE_GNOME_PRINT_TO_ENGINE (footer_height),
+			footer_height, footer_print, user_data);
+
 	html_painter_end (painter);
 }
 
 static gint
 print_all_pages (HTMLPainter *painter,
 		 HTMLEngine *engine,
-		 gdouble header_height, gdouble footer_height,
-		 GtkHTMLPrintCallback header_print, GtkHTMLPrintCallback footer_print, gpointer user_data, gboolean do_print)
+		 gdouble header_height,
+		 gdouble footer_height,
+		 GtkHTMLPrintCallback header_print,
+		 GtkHTMLPrintCallback footer_print,
+		 gpointer user_data,
+		 gboolean do_print)
 {
 	HTMLPrinter *printer = HTML_PRINTER (painter);
 	gint new_split_offset, split_offset;
 	gint document_height;
 	gint pages = 0;
-	gint page_width, page_height, body_height;
+	gint page_width;
+	gint page_height;
+	gint body_height;
 
 	page_height = html_printer_get_page_height (printer);
-	page_width  = html_printer_get_page_width (printer);
+	page_width = html_printer_get_page_width (printer);
 
 	if (header_height + footer_height >= page_height) {
 		header_print = footer_print = NULL;
-		g_warning ("Page header height + footer height >= page height, disabling header/footer printing");
+		g_warning ("Page header height + footer height >= "
+		           "page height, disabling header/footer printing");
 	}
 
-	body_height = page_height - SCALE_GNOME_PRINT_TO_ENGINE (header_height + footer_height);
+	body_height = page_height -
+		SCALE_GNOME_PRINT_TO_ENGINE (header_height + footer_height);
 	split_offset = 0;
 	
 	document_height = html_engine_get_doc_height (engine);
 
 	do {
 		pages ++;
-		new_split_offset = html_object_check_page_split (engine->clue, painter,
-								 split_offset + body_height);
+		new_split_offset = html_object_check_page_split (
+			engine->clue, painter, split_offset + body_height);
 
-		if (new_split_offset <= split_offset
-		    || new_split_offset - split_offset < engine->min_split_index * body_height)
+		if (new_split_offset <= split_offset ||
+		    new_split_offset - split_offset <
+		    engine->min_split_index * body_height)
 			new_split_offset = split_offset + body_height;
 
 		if (do_print)
-			print_page   (painter, engine, split_offset,
-				      page_width, page_height,
-				      new_split_offset - split_offset,
-				      header_height, footer_height, header_print, footer_print, user_data);
+			print_page (
+				painter, engine, split_offset, page_width,
+				page_height, new_split_offset - split_offset,
+				header_height, footer_height, header_print,
+				footer_print, user_data);
 			
 		split_offset = new_split_offset;
-		
-	}  while (split_offset < document_height); 
-	return pages;
-}
 
-static gboolean
-do_we_have_default_font (HTMLPainter *painter)
-{
-	HTMLFont *font;
-	gboolean rv = FALSE;
+	} while (split_offset < document_height); 
 
-	font = html_painter_get_font (painter, NULL, GTK_HTML_FONT_STYLE_DEFAULT);
-	if (font) {
-		rv = TRUE;
-	}
-
-	return rv;
+	return pages;
 }
 
 static gint
 print_with_header_footer (HTMLEngine *engine, 
-				      GtkPrintContext *print_context,
-				      gdouble header_height, 
-				      gdouble footer_height,
-				      GtkHTMLPrintCallback header_print, 
-				      GtkHTMLPrintCallback footer_print, 
-				      gpointer user_data, gboolean do_print)
+			  GtkPrintContext *context,
+			  gdouble header_height, 
+			  gdouble footer_height,
+			  GtkHTMLPrintCallback header_print, 
+			  GtkHTMLPrintCallback footer_print, 
+			  gpointer user_data,
+			  gboolean do_print)
 {
 	HTMLPainter *printer;
-	HTMLPainter *old_painter;
+	HTMLFont *default_font;
 	gint pages = 0;
 
 	g_return_val_if_fail (engine->clue != NULL, 0);
   
-	printer = html_printer_new (GTK_WIDGET (engine->widget), print_context);
+	printer = html_printer_new (
+		GTK_WIDGET (engine->widget), context);
 	gtk_html_set_fonts (engine->widget, printer);
 
-	if (do_we_have_default_font (printer)) {
+	default_font = html_painter_get_font (
+		printer, NULL, GTK_HTML_FONT_STYLE_DEFAULT);
+
+	if (default_font != NULL) {
+		HTMLPainter *old_painter;
 		gint min_width, page_width;
-		old_painter = engine->painter;
 
-		g_object_ref (old_painter);
+		old_painter = g_object_ref (engine->painter);
+
 		html_engine_set_painter (engine, printer);
 
 		min_width = html_engine_calc_min_width (engine);
-		page_width = html_painter_get_page_width (engine->painter, engine);
-		/* printf ("min_width %d page %d\n", min_width, page_width); */
+		page_width = html_painter_get_page_width (
+			engine->painter, engine);
 		if (min_width > page_width) {
-			html_printer_set_scale (HTML_PRINTER (printer), MAX (0.5, ((gdouble) page_width) / min_width)); 
-			html_font_manager_clear_font_cache (&printer->font_manager);
-			html_object_change_set_down (engine->clue, HTML_CHANGE_ALL);
+			html_printer_set_scale (
+				HTML_PRINTER (printer),
+				MAX (0.5, ((gdouble) page_width) / min_width)); 
+			html_font_manager_clear_font_cache (
+				&printer->font_manager);
+			html_object_change_set_down (
+				engine->clue, HTML_CHANGE_ALL);
 			html_engine_calc_size (engine, NULL);
-			/* printf ("scale %lf\n", HTML_PRINTER (printer)->scale); */
-			/*  gtk_html_debug_dump_tree (engine->clue, 0); */
 		}
 	
-		pages = print_all_pages (HTML_PAINTER (printer), engine,
-					 header_height, footer_height,
-					 header_print, footer_print,
-					 user_data, do_print);
-		
+		pages = print_all_pages (
+			HTML_PAINTER (printer), engine, header_height,
+			footer_height, header_print, footer_print,
+			user_data, do_print);
+
 		html_engine_set_painter (engine, old_painter);
-		g_object_unref (G_OBJECT (old_painter));
+		g_object_unref (old_painter);
 	} else {
 		/* TODO2 dialog instead of warning */
 		g_warning (_("Cannot allocate default font for printing\n"));
 	}
 
-	g_object_unref (G_OBJECT (printer));
+	g_object_unref (printer);
 
 	return pages;
 }
 
 void
-html_engine_print (HTMLEngine *engine, GtkPrintContext *print_context)
+html_engine_print (HTMLEngine *engine, 
+		   GtkPrintContext *context,
+		   gdouble header_height, 
+		   gdouble footer_height,
+		   GtkHTMLPrintCallback header_print, 
+		   GtkHTMLPrintCallback footer_print, 
+		   gpointer user_data)
 {
-	html_engine_print_with_header_footer (engine, print_context, .0, .0, NULL, NULL, NULL);
+	print_with_header_footer (
+		engine, context, header_height, footer_height,
+		header_print, footer_print, user_data, TRUE);
+}
+
+gint
+html_engine_print_get_pages_num (HTMLEngine *engine,
+				 GtkPrintContext *context,
+				 gdouble header_height,
+				 gdouble footer_height)
+{
+	return print_with_header_footer (
+		engine, context, header_height, footer_height,
+		NULL, NULL, NULL, FALSE);
 }
 
 void
-html_engine_print_with_header_footer (HTMLEngine *engine, 
-				      GtkPrintContext *print_context,
-				      gdouble header_height, 
-				      gdouble footer_height,
-				      GtkHTMLPrintCallback header_print, 
-				      GtkHTMLPrintCallback footer_print, 
-				      gpointer user_data)
+html_engine_print_set_min_split_index (HTMLEngine *engine, gdouble index)
 {
-	print_with_header_footer (engine, print_context,
-				  header_height, footer_height, header_print, footer_print, user_data, TRUE);
+	engine->min_split_index = index;
 }
 
-gint
-html_engine_print_get_pages_num (HTMLEngine *e, GtkPrintContext *print_context,
-				 gdouble header_height, gdouble footer_height)
+
+typedef struct {
+	HTMLEngine *engine;
+	HTMLPainter *painter;
+	HTMLPainter *old_painter;
+	GtkHTMLPrintCalcHeight calc_header_height;
+	GtkHTMLPrintCalcHeight calc_footer_height;
+	GtkHTMLPrintDrawFunc draw_header;
+	GtkHTMLPrintDrawFunc draw_footer;
+	gint header_height;
+	gint footer_height;
+	gpointer user_data;
+	GArray *offsets;
+} EnginePrintData;
+
+static void
+engine_print_begin_print (GtkPrintOperation *operation,
+                          GtkPrintContext *context,
+                          EnginePrintData *data)
+{
+	HTMLPrinter *printer;
+	HTMLFont *default_font;
+	gint min_width;
+	gint page_width;
+	gint page_height;
+	gint body_height;
+	gint document_height;
+	gint new_split_offset;
+	gint split_offset;
+
+	data->painter = html_printer_new (
+		GTK_WIDGET (data->engine->widget), context);
+	gtk_html_set_fonts (data->engine->widget, data->painter);
+
+	data->offsets = g_array_new (FALSE, TRUE, sizeof (gint));
+
+	default_font = html_painter_get_font (
+		data->painter, NULL, GTK_HTML_FONT_STYLE_DEFAULT);
+
+	if (default_font == NULL)
+		g_warning (_("Cannot allocate default font for printing"));
+
+	data->old_painter = g_object_ref (data->engine->painter);
+	html_engine_set_painter (data->engine, data->painter);
+
+	printer = HTML_PRINTER (data->painter);
+
+	min_width = html_engine_calc_min_width (data->engine);
+	page_width = html_painter_get_page_width (
+		data->engine->painter, data->engine);
+	if (min_width > page_width) {
+		html_printer_set_scale (
+			printer, MAX (0.5,
+			((gdouble) page_width) / min_width));
+		html_font_manager_clear_font_cache (
+			&data->painter->font_manager);
+		html_object_change_set_down (
+			data->engine->clue, HTML_CHANGE_ALL);
+		html_engine_calc_size (data->engine, NULL);
+	}
+
+	page_height = html_printer_get_page_height (printer);
+	page_width = html_printer_get_page_width (printer);
+
+	if (data->calc_header_height != NULL)
+		data->header_height = data->calc_header_height (
+			GTK_HTML (data->engine->widget),
+			operation, context, data->user_data);
+	else
+		data->header_height = 0;
+
+	if (data->calc_footer_height != NULL)
+		data->footer_height = data->calc_footer_height (
+			GTK_HTML (data->engine->widget),
+			operation, context, data->user_data);
+	else
+		data->footer_height = 0;
+
+	if (data->header_height + data->footer_height >= page_height) {
+		data->draw_header = data->draw_footer = NULL;
+		g_warning ("Page header height + footer height >= "
+		           "page height, disabling header/footer printing");
+	}
+
+	/* Calculate page offsets */
+
+	body_height =
+		page_height - (data->header_height + data->footer_height);
+	document_height = html_engine_get_doc_height (data->engine);
+
+	split_offset = 0;
+	g_array_append_val (data->offsets, split_offset);
+
+	do {
+		new_split_offset = html_object_check_page_split (
+			data->engine->clue, data->painter,
+			split_offset + body_height);
+
+		if (new_split_offset <= split_offset ||
+		    new_split_offset - split_offset <
+		    data->engine->min_split_index * body_height)
+			new_split_offset = split_offset + body_height;
+
+		split_offset = new_split_offset;
+		g_array_append_val (data->offsets, split_offset);
+
+	} while (split_offset < document_height);
+
+	gtk_print_operation_set_n_pages (operation, data->offsets->len - 1);
+}
+
+static void
+engine_print_draw_page (GtkPrintOperation *operation,
+                        GtkPrintContext *context,
+                        gint page_nr,
+                        EnginePrintData *data)
+{
+	HTMLPainter *painter = data->painter;
+	HTMLPrinter *printer = HTML_PRINTER (painter);
+	PangoRectangle rec;
+	gint page_width;
+	gint page_height;
+	gint body_height;
+	gint offset[2];
+	cairo_t *cr;
+
+	g_assert (data->offsets->len > page_nr);
+	offset[0] = g_array_index (data->offsets, gint, page_nr + 0);
+	offset[1] = g_array_index (data->offsets, gint, page_nr + 1);
+
+	page_width = html_printer_get_page_width (printer);
+	page_height = html_printer_get_page_height (printer);
+	body_height = offset[1] - offset[0];
+
+	cr = gtk_print_context_get_cairo_context (context);
+
+	html_painter_begin (painter, 0, 0, page_width, page_height);
+
+	/* Draw Header */
+	if (data->draw_header != NULL) {
+		rec.x = 0;
+		rec.y = 0;
+		rec.width = page_width;
+		rec.height = data->header_height;
+		cairo_save (cr);
+		html_painter_set_clip_rectangle (
+			painter, rec.x, rec.y, rec.width, rec.height);
+#ifdef CLIP_DEBUG
+		html_painter_draw_rect (
+			painter, rec.x, rec.y, rec.width, rec.height);
+#endif
+		data->draw_header (
+			GTK_HTML (data->engine->widget), operation,
+			context, page_nr, &rec, data->user_data);
+		cairo_restore (cr);
+	}
+
+	/* Draw Body */
+	rec.x = 0;
+	rec.y = data->header_height;
+	rec.width = page_width;
+	rec.height = body_height;
+	cairo_save (cr);
+	html_painter_set_clip_rectangle (
+		painter, rec.x, rec.y, rec.width, rec.height);
+#ifdef CLIP_DEBUG
+	html_painter_draw_rect (
+		painter, rec.x, rec.y, rec.width, rec.height);
+#endif
+	html_object_draw (
+		data->engine->clue, painter, 0, offset[0], page_width,
+		body_height, 0, -offset[0] + data->header_height);
+	cairo_restore (cr);
+
+	/* Draw Footer */
+	if (data->draw_footer != NULL) {
+		rec.x = 0;
+		rec.y = page_height - data->footer_height;
+		rec.width = page_width;
+		rec.height = data->footer_height;
+		cairo_save (cr);
+		html_painter_set_clip_rectangle (
+			painter, rec.x, rec.y, rec.width, rec.height);
+#ifdef CLIP_DEBUG
+		html_painter_draw_rect (
+			painter, rec.x, rec.y, rec.width, rec.height);
+#endif
+		data->draw_footer (
+			GTK_HTML (data->engine->widget), operation,
+			context, page_nr, &rec, data->user_data);
+		cairo_restore (cr);
+	}
+
+	html_painter_end (painter);
+}
+
+static void
+engine_print_end_print (GtkPrintOperation *operation,
+                        GtkPrintContext *context,
+                        EnginePrintData *data)
 {
-	return print_with_header_footer (e, print_context, header_height, footer_height, NULL, NULL, NULL, FALSE);
+	html_engine_set_painter (data->engine, data->old_painter);
+
+	g_object_unref (data->painter);
+	g_object_unref (data->old_painter);
+	g_array_free (data->offsets, TRUE);
 }
 
-void
-html_engine_print_set_min_split_index (HTMLEngine *e, gdouble idx)
+GtkPrintOperationResult
+html_engine_print_operation_run (HTMLEngine *engine,
+                                 GtkPrintOperation *operation,
+				 GtkPrintOperationAction action,
+				 GtkWindow *parent,
+				 GtkHTMLPrintCalcHeight calc_header_height,
+				 GtkHTMLPrintCalcHeight calc_footer_height,
+                                 GtkHTMLPrintDrawFunc draw_header,
+                                 GtkHTMLPrintDrawFunc draw_footer,
+                                 gpointer user_data,
+				 GError **error)
 {
-	e->min_split_index = idx;
+	EnginePrintData data;
+ 
+	g_return_val_if_fail (
+		engine != NULL, GTK_PRINT_OPERATION_RESULT_ERROR);
+	g_return_val_if_fail (
+		operation != NULL, GTK_PRINT_OPERATION_RESULT_ERROR);
+
+	data.engine = engine;
+	data.calc_header_height = calc_header_height;
+	data.calc_footer_height = calc_footer_height;
+	data.draw_header = draw_header;
+	data.draw_footer = draw_footer;
+	data.user_data = user_data;
+
+	g_signal_connect (
+		operation, "begin-print",
+		G_CALLBACK (engine_print_begin_print), &data);
+	g_signal_connect (
+		operation, "draw-page",
+		G_CALLBACK (engine_print_draw_page), &data);
+	g_signal_connect (
+		operation, "end-print",
+		G_CALLBACK (engine_print_end_print), &data);
+
+	return gtk_print_operation_run (operation, action, parent, error);
 }
Index: src/htmlengine-print.h
===================================================================
--- src/htmlengine-print.h	(revision 8475)
+++ src/htmlengine-print.h	(working copy)
@@ -25,19 +25,30 @@
 #include <gtk/gtk.h>
 #include "htmlengine.h"
 
-void  html_engine_print                     (HTMLEngine           *e,
-					     GtkPrintContext    *print_context);
-void  html_engine_print_with_header_footer  (HTMLEngine           *e,
-					     GtkPrintContext    *print_context,
-					     gdouble               header_height,
-					     gdouble               footer_height,
-					     GtkHTMLPrintCallback  header_print,
-					     GtkHTMLPrintCallback  footer_print,
-					     gpointer              user_data);
-gint  html_engine_print_get_pages_num       (HTMLEngine           *e,
-					     GtkPrintContext    *print_context,
-					     gdouble               header_height,
-					     gdouble               footer_height);
-void  html_engine_print_set_min_split_index (HTMLEngine           *e,
-					     gdouble               idx);
+void	html_engine_print		(HTMLEngine *engine,
+					 GtkPrintContext *context,
+					 gdouble header_height,
+					 gdouble footer_height,
+					 GtkHTMLPrintCallback header_print,
+					 GtkHTMLPrintCallback footer_print,
+					 gpointer user_data);
+gint	html_engine_print_get_pages_num	(HTMLEngine *engine,
+					 GtkPrintContext *context,
+					 gdouble header_height,
+					 gdouble footer_height);
+void	html_engine_print_set_min_split_index	(HTMLEngine *engine,
+						 gdouble index);
+
+GtkPrintOperationResult
+html_engine_print_operation_run	   (HTMLEngine *engine,
+				    GtkPrintOperation *operation,
+				    GtkPrintOperationAction action,
+				    GtkWindow *parent,
+				    GtkHTMLPrintCalcHeight calc_header_height,
+				    GtkHTMLPrintCalcHeight calc_footer_height,
+				    GtkHTMLPrintDrawFunc draw_header,
+				    GtkHTMLPrintDrawFunc draw_footer,
+				    gpointer user_data,
+				    GError **error);
+
 #endif
Index: src/htmlprinter.c
===================================================================
--- src/htmlprinter.c	(revision 8475)
+++ src/htmlprinter.c	(working copy)
@@ -36,7 +36,7 @@
 /* #define PRINTER_DEBUG */
 
 
-static HTMLPainterClass *parent_class = NULL;
+static gpointer parent_class = NULL;
 
 
 /* The size of a pixel in the printed output, in points.  */
@@ -46,6 +46,7 @@ static gdouble
 printer_get_page_height (HTMLPrinter *printer)
 {
 	GtkPageSetup *page_setup;
+
 	page_setup = gtk_print_context_get_page_setup (printer->context);
 	return gtk_page_setup_get_page_height (page_setup, GTK_UNIT_POINTS);
 }
@@ -54,42 +55,9 @@ static gdouble
 printer_get_page_width (HTMLPrinter *printer)
 {
 	GtkPageSetup *page_setup;
-	page_setup = gtk_print_context_get_page_setup (printer->context);
-	return gtk_page_setup_get_page_width (page_setup, GTK_UNIT_POINTS);
-}
-
-static gdouble
-get_lmargin (HTMLPrinter *printer)
-{
-	GtkPageSetup *page_setup;
-
-	page_setup = gtk_print_context_get_page_setup (printer->context);
-	return gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_POINTS);
-}
-
-static gdouble
-get_rmargin (HTMLPrinter *printer)
-{
-	GtkPageSetup *page_setup;
 
 	page_setup = gtk_print_context_get_page_setup (printer->context);
-	return gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_POINTS);
-}
-
-static gdouble
-get_tmargin (HTMLPrinter *printer)
-{
-	GtkPageSetup *page_setup;
-	page_setup = gtk_print_context_get_page_setup (printer->context);
-	return gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_POINTS);
-}
-
-static gdouble
-get_bmargin (HTMLPrinter *printer)
-{
-	GtkPageSetup *page_setup;
-	page_setup = gtk_print_context_get_page_setup (printer->context);
-	return gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_POINTS); 
+	return gtk_page_setup_get_page_width (page_setup, GTK_UNIT_POINTS);
 }
 
 gdouble
@@ -98,36 +66,6 @@ html_printer_scale_to_gnome_print (HTMLP
 	return SCALE_ENGINE_TO_GNOME_PRINT (x);
 }
 
-void
-html_printer_coordinates_to_gnome_print (HTMLPrinter *printer,
-					 gint engine_x, gint engine_y,
-					 gdouble *print_x_return, gdouble *print_y_return)
-{
-	gdouble print_x, print_y;
-
-	print_x = SCALE_ENGINE_TO_GNOME_PRINT (engine_x);
-	print_y = SCALE_ENGINE_TO_GNOME_PRINT (engine_y);
-
-	print_x = print_x + get_lmargin (printer);
-
-	*print_x_return = print_x;
-	*print_y_return = print_y;
-}
-
-#if 0
-static void
-gnome_print_coordinates_to_engine (HTMLPrinter *printer,
-				   gdouble print_x, gdouble print_y,
-				   gint *engine_x_return, gint *engine_y_return)
-{
-	print_x -= get_lmargin (printer);
-	print_y -= get_bmargin (printer);
-
-	*engine_x_return = SCALE_ENGINE_TO_GNOME_PRINT (print_x);
-	*engine_y_return = SCALE_GNOME_PRINT_TO_ENGINE (get_page_height (printer) - print_y);
-}
-#endif
-
 
 /* GtkObject methods.  */
 
@@ -143,7 +81,7 @@ finalize (GObject *object)
 		printer->context = NULL;
 	}
 
-	(* G_OBJECT_CLASS (parent_class)->finalize) (object);
+	G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
 
@@ -167,9 +105,10 @@ begin (HTMLPainter *painter,
 
 	cr = gtk_print_context_get_cairo_context (pc);
 	cairo_save (cr);
-       
-	html_printer_coordinates_to_gnome_print (printer, x1, y1, &printer_x1, &printer_y1);
-	
+ 
+	printer_x1 = SCALE_ENGINE_TO_GNOME_PRINT (x1);
+	printer_y1 = SCALE_ENGINE_TO_GNOME_PRINT (y1);
+
 	printer_x2 = printer_x1 + SCALE_ENGINE_TO_GNOME_PRINT (x2);
 	printer_y2 = SCALE_ENGINE_TO_GNOME_PRINT (y2);
 
@@ -191,14 +130,6 @@ begin (HTMLPainter *painter,
 static void
 end (HTMLPainter *painter)
 {
-	HTMLPrinter *printer;
-	cairo_t *cr;
-
-	printer = HTML_PRINTER (painter);
-	g_return_if_fail (printer->context != NULL);
-
-	cr = gtk_print_context_get_cairo_context (printer->context);
-	cairo_show_page (cr);
 }
 
 static void
@@ -253,7 +184,8 @@ prepare_rectangle (HTMLPainter *painter,
 
 	width = SCALE_ENGINE_TO_GNOME_PRINT (w);
 	height = SCALE_ENGINE_TO_GNOME_PRINT (h);
-	html_printer_coordinates_to_gnome_print (HTML_PRINTER (painter), _x, _y, &x, &y);
+	x = SCALE_ENGINE_TO_GNOME_PRINT (_x);
+	y = SCALE_ENGINE_TO_GNOME_PRINT (_y);
 	cr = gtk_print_context_get_cairo_context (context);
 	cairo_new_path (cr);
 	cairo_rectangle (cr, x, y, x + width, y + height );
@@ -299,8 +231,10 @@ draw_line (HTMLPainter *painter,
 	printer = HTML_PRINTER (painter);
 	g_return_if_fail (printer->context != NULL);
 
-	html_printer_coordinates_to_gnome_print (printer, x1, y1, &printer_x1, &printer_y1);
-	html_printer_coordinates_to_gnome_print (printer, x2, y2, &printer_x2, &printer_y2);
+	printer_x1 = SCALE_ENGINE_TO_GNOME_PRINT (x1);
+	printer_y1 = SCALE_ENGINE_TO_GNOME_PRINT (y1);
+	printer_x2 = SCALE_ENGINE_TO_GNOME_PRINT (x2);
+	printer_y2 = SCALE_ENGINE_TO_GNOME_PRINT (y2);
 
 	cr = gtk_print_context_get_cairo_context (printer->context);
 	cairo_set_line_width (cr, PIXEL_SIZE); 
@@ -370,8 +304,8 @@ draw_border (HTMLPainter *painter,
 	height = SCALE_ENGINE_TO_GNOME_PRINT (h);
 	bs     = SCALE_ENGINE_TO_GNOME_PRINT (bordersize);
 
-
-	html_printer_coordinates_to_gnome_print (HTML_PRINTER (painter), _x, _y, &x, &y);
+	x = SCALE_ENGINE_TO_GNOME_PRINT (_x);
+	y = SCALE_ENGINE_TO_GNOME_PRINT (_y);
 
 	cr = gtk_print_context_get_cairo_context (pc);
 	if (col2)
@@ -420,7 +354,8 @@ draw_background (HTMLPainter *painter,
 
 	width = SCALE_ENGINE_TO_GNOME_PRINT  (pix_width);
 	height = SCALE_ENGINE_TO_GNOME_PRINT (pix_height);
-	html_printer_coordinates_to_gnome_print (printer, ix, iy, &x, &y);
+	x = SCALE_ENGINE_TO_GNOME_PRINT (ix);
+	y = SCALE_ENGINE_TO_GNOME_PRINT (iy);
 
 	if (color) {
 		cr = gtk_print_context_get_cairo_context (pc);
@@ -468,7 +403,8 @@ draw_pixmap (HTMLPainter *painter, GdkPi
        	g_return_if_fail (printer->context != NULL);
 	cr = gtk_print_context_get_cairo_context (printer->context);
 	page_height =(double) gtk_print_context_get_height (printer->context);
-	html_printer_coordinates_to_gnome_print (printer, x, y, &print_x, &print_y);
+	print_x = SCALE_ENGINE_TO_GNOME_PRINT (x);
+	print_y = SCALE_ENGINE_TO_GNOME_PRINT (y);
 	print_scale_width  = SCALE_ENGINE_TO_GNOME_PRINT (scale_width);
 	print_scale_height = SCALE_ENGINE_TO_GNOME_PRINT (scale_height);
 	cairo_save (cr);
@@ -492,7 +428,8 @@ fill_rect (HTMLPainter *painter, gint x,
 	printer_width = SCALE_ENGINE_TO_GNOME_PRINT (width);
 	printer_height = SCALE_ENGINE_TO_GNOME_PRINT (height);
 
-	html_printer_coordinates_to_gnome_print (printer, x, y, &printer_x, &printer_y);
+	printer_x = SCALE_ENGINE_TO_GNOME_PRINT (x);
+	printer_y = SCALE_ENGINE_TO_GNOME_PRINT (y);
 
 	cr = gtk_print_context_get_cairo_context (printer->context);
 	cairo_new_path (cr);
@@ -516,8 +453,8 @@ draw_lines (HTMLPrinter *printer, double
 
 	if (properties->underline) {
 #ifdef PANGO_1_5_OR_HIGHER
-		double thickness = SCALE_PANGO_TO_GNOME_PRINT (pango_font_metrics_get_underline_thickness (metrics));
-		double position = SCALE_PANGO_TO_GNOME_PRINT (pango_font_metrics_get_underline_position (metrics));
+		double thickness = pango_units_to_double (pango_font_metrics_get_underline_thickness (metrics));
+		double position = pango_units_to_double (pango_font_metrics_get_underline_position (metrics));
 		double ly = y + position - thickness / 2;
 #else
 		double thickness = 1.0;
@@ -533,12 +470,12 @@ draw_lines (HTMLPrinter *printer, double
 		
 	if (properties->strikethrough) {
 #ifdef PANGO_1_5_OR_HIGHER
-		double thickness = SCALE_PANGO_TO_GNOME_PRINT (pango_font_metrics_get_strikethrough_thickness (metrics));
-		double position = SCALE_PANGO_TO_GNOME_PRINT (pango_font_metrics_get_strikethrough_position (metrics));
+		double thickness = pango_units_to_double (pango_font_metrics_get_strikethrough_thickness (metrics));
+		double position = pango_units_to_double (pango_font_metrics_get_strikethrough_position (metrics));
 		double ly = y + position - thickness / 2;
 #else
 		double thickness = 1.0;
-		double position = SCALE_PANGO_TO_GNOME_PRINT (pango_font_metrics_get_ascent (metrics)/3);
+		double position = pango_units_to_double (pango_font_metrics_get_ascent (metrics)/3);
 		double ly = y + position - thickness / 2;
 #endif
 		cairo_new_path (cr);
@@ -560,8 +497,8 @@ draw_glyphs (HTMLPainter *painter, gint 
 
 	printer = HTML_PRINTER (painter);
 
-
-	html_printer_coordinates_to_gnome_print (printer, x, y, &print_x, &print_y);
+	print_x = SCALE_ENGINE_TO_GNOME_PRINT (x);
+	print_y = SCALE_ENGINE_TO_GNOME_PRINT (y);
 
 	cr = gtk_print_context_get_cairo_context (printer->context);
 
@@ -577,9 +514,9 @@ draw_glyphs (HTMLPainter *painter, gint 
 					 properties.bg_color->blue / 65535.0);
 		        cairo_rectangle (cr,
 					 print_x,
-					 print_y + SCALE_PANGO_TO_GNOME_PRINT (log_rect.y + log_rect.height),
-					 SCALE_PANGO_TO_GNOME_PRINT (log_rect.width),
-					 SCALE_PANGO_TO_GNOME_PRINT (log_rect.height));
+					 print_y + pango_units_to_double (log_rect.y + log_rect.height),
+					 pango_units_to_double (log_rect.width),
+					 pango_units_to_double (log_rect.height));
 			cairo_fill (cr);
 	}
 	
@@ -596,7 +533,7 @@ draw_glyphs (HTMLPainter *painter, gint 
 				       item->analysis.font,
 				       glyphs);  
 	draw_lines (printer, print_x, print_y,
-		    SCALE_PANGO_TO_GNOME_PRINT (log_rect.width),
+		    pango_units_to_double (log_rect.width),
 		    &item->analysis, &properties);
 	
 	cairo_restore (cr);
@@ -611,7 +548,9 @@ draw_embedded (HTMLPainter *p, HTMLEmbed
 	GtkWidget *embedded_widget;
 	cairo_t *cr;
 
-	html_printer_coordinates_to_gnome_print (printer, x, y, &print_x, &print_y);
+	print_x = SCALE_ENGINE_TO_GNOME_PRINT (x);
+	print_y = SCALE_ENGINE_TO_GNOME_PRINT (y);
+
 	cr = gtk_print_context_get_cairo_context (printer->context);
 	cairo_save (cr);
 	cairo_translate (cr, 
@@ -659,27 +598,23 @@ get_page_height (HTMLPainter *painter, H
 }
 
 static void
-html_printer_init (GObject *object)
+html_printer_init (HTMLPrinter *printer)
 {
-	HTMLPrinter *printer;
-
-	printer                = HTML_PRINTER (object);
-	printer->context       = NULL;
-
 	html_printer_set_scale (printer, 1.0);
 }
 
 static void
-html_printer_class_init (GObjectClass *object_class)
+html_printer_class_init (HTMLPrinterClass *class)
 {
+	GObjectClass *object_class;
 	HTMLPainterClass *painter_class;
 
-	painter_class = HTML_PAINTER_CLASS (object_class);
-
-	parent_class = g_type_class_ref (HTML_TYPE_PAINTER);
+	parent_class = g_type_class_peek_parent (class);
 
+	object_class = G_OBJECT_CLASS (class);
 	object_class->finalize = finalize;
 
+	painter_class = HTML_PAINTER_CLASS (class);
 	painter_class->begin = begin;
 	painter_class->end = end;
 	painter_class->alloc_color = alloc_color;
@@ -705,24 +640,27 @@ html_printer_class_init (GObjectClass *o
 GType
 html_printer_get_type (void)
 {
-	static GType html_printer_type = 0;
+	static GType type = 0;
 
-	if (html_printer_type == 0) {
-		static const GTypeInfo html_printer_info = {
+	if (G_UNLIKELY (type == 0)) {
+		static const GTypeInfo type_info = {
 			sizeof (HTMLPrinterClass),
-			NULL,
-			NULL,
+			(GBaseInitFunc) NULL,
+			(GBaseFinalizeFunc) NULL,
 			(GClassInitFunc) html_printer_class_init,
-			NULL,
-			NULL,
+			(GClassFinalizeFunc) NULL,
+			NULL,  /* class_data */
 			sizeof (HTMLPrinter),
-			1,
+			0,     /* n_preallocs */
 			(GInstanceInitFunc) html_printer_init,
+			NULL   /* value_table */
 		};
-		html_printer_type = g_type_register_static (HTML_TYPE_PAINTER, "HTMLPrinter", &html_printer_info, 0);
+
+		type = g_type_register_static (
+			HTML_TYPE_PAINTER, "HTMLPrinter", &type_info, 0);
 	}
 
-	return html_printer_type;
+	return type;
 }
 
 HTMLPainter *
@@ -754,7 +692,7 @@ html_printer_get_page_width (HTMLPrinter
 	g_return_val_if_fail (printer != NULL, 0);
 	g_return_val_if_fail (HTML_IS_PRINTER (printer), 0);
 
-	printer_width = printer_get_page_width (printer) - get_lmargin (printer) - get_rmargin (printer);
+	printer_width = printer_get_page_width (printer);
 	engine_width = SCALE_GNOME_PRINT_TO_ENGINE (printer_width);
 
 	return engine_width;
@@ -769,7 +707,7 @@ html_printer_get_page_height (HTMLPrinte
 	g_return_val_if_fail (printer != NULL, 0);
 	g_return_val_if_fail (HTML_IS_PRINTER (printer), 0);
 
-	printer_height = printer_get_page_height (printer) + get_tmargin (printer) + get_bmargin (printer);
+	printer_height = printer_get_page_height (printer);
 	engine_height = SCALE_GNOME_PRINT_TO_ENGINE (printer_height);
 
 	return engine_height;
@@ -794,5 +732,5 @@ html_printer_set_scale (HTMLPrinter *pri
 	painter = HTML_PAINTER (printer);
 	
 	printer->scale = scale;
-	painter->engine_to_pango = scale * PANGO_SCALE / 1024.;
+	painter->engine_to_pango = scale;
 }
Index: src/gtkhtml.c
===================================================================
--- src/gtkhtml.c	(revision 8475)
+++ src/gtkhtml.c	(working copy)
@@ -4167,25 +4167,29 @@ gtk_html_get_base (GtkHTML *html)
 
 /* Printing.  */
 void
-gtk_html_print_page (GtkHTML *html,
-		GtkPrintContext *print_context)
+gtk_html_print_page (GtkHTML *html, GtkPrintContext *context)
 {
 	g_return_if_fail (html != NULL);
 	g_return_if_fail (GTK_IS_HTML (html));
 
-	html_engine_print (html->engine, print_context);
+	html_engine_print (html->engine, context, .0, .0, NULL, NULL, NULL);
 }
 	
 void
-gtk_html_print_page_with_header_footer (GtkHTML *html, GtkPrintContext *print_context,
-					gdouble header_height, gdouble footer_height,
-					GtkHTMLPrintCallback header_print, GtkHTMLPrintCallback footer_print, gpointer user_data)
+gtk_html_print_page_with_header_footer (GtkHTML *html,
+					GtkPrintContext *context,
+					gdouble header_height,
+					gdouble footer_height,
+					GtkHTMLPrintCallback header_print,
+					GtkHTMLPrintCallback footer_print,
+					gpointer user_data)
 {
 	g_return_if_fail (html != NULL);
 	g_return_if_fail (GTK_IS_HTML (html));
 
-	html_engine_print_with_header_footer (html->engine, print_context,
-					      header_height, footer_height, header_print, footer_print, user_data);
+	html_engine_print (
+		html->engine, context, header_height, footer_height,
+		header_print, footer_print, user_data);
 }
 
 
@@ -5879,9 +5883,31 @@ gtk_html_set_images_blocking (GtkHTML *h
 }
 
 gint
-gtk_html_print_page_get_pages_num (GtkHTML *html, GtkPrintContext *print_context, gdouble header_height, gdouble footer_height)
-{
-	return html_engine_print_get_pages_num (html->engine, print_context, header_height, footer_height);
+gtk_html_print_page_get_pages_num (GtkHTML *html,
+				   GtkPrintContext *context,
+				   gdouble header_height,
+				   gdouble footer_height)
+{
+	return html_engine_print_get_pages_num (
+		html->engine, context, header_height, footer_height);
+}
+
+GtkPrintOperationResult
+gtk_html_print_operation_run (GtkHTML *html,
+                              GtkPrintOperation *operation,
+                              GtkPrintOperationAction action,
+                              GtkWindow *parent,
+			      GtkHTMLPrintCalcHeight calc_header_height,
+			      GtkHTMLPrintCalcHeight calc_footer_height,
+                              GtkHTMLPrintDrawFunc draw_header,
+                              GtkHTMLPrintDrawFunc draw_footer,
+                              gpointer user_data,
+                              GError **error)
+{
+	return html_engine_print_operation_run (
+		html->engine, operation, action, parent,
+		calc_header_height, calc_footer_height,
+		draw_header, draw_footer, user_data, error);
 }
 
 gboolean
Index: src/htmlprinter.h
===================================================================
--- src/htmlprinter.h	(revision 8475)
+++ src/htmlprinter.h	(working copy)
@@ -36,7 +36,6 @@
 
 #define SCALE_ENGINE_TO_GNOME_PRINT(x) ((printer->scale * (x)) / 1024.0)
 #define SCALE_GNOME_PRINT_TO_ENGINE(x) ((gint) (((x) * 1024.0 / printer->scale) + 0.5))
-#define SCALE_PANGO_TO_GNOME_PRINT(x) (((x)) / 1024.0)
 
 struct _HTMLPrinter {
 	HTMLPainter base;
@@ -56,11 +55,6 @@ guint        html_printer_get_page_width
 guint        html_printer_get_page_height             (HTMLPrinter       *printer);
 gdouble      html_printer_scale_to_gnome_print        (HTMLPrinter       *printer,
 						       gint               x);
-void         html_printer_coordinates_to_gnome_print  (HTMLPrinter       *printer,
-						       gint               engine_x,
-						       gint               engine_y,
-						       gdouble           *print_x_return,
-						       gdouble           *print_y_return);
 void         html_printer_set_scale                   (HTMLPrinter       *printer,
 						       gdouble            scale);
 
Index: src/gtkhtml.h
===================================================================
--- src/gtkhtml.h	(revision 8475)
+++ src/gtkhtml.h	(working copy)
@@ -214,19 +214,29 @@ void                       gtk_html_set_
 gboolean                   gtk_html_get_animate                   (const GtkHTML             *html);
 
 /* Printing support.  */
-void 			   gtk_html_print_page_with_header_footer  (GtkHTML		     *html,
-								   GtkPrintContext 	     *print_context,
+void 			   gtk_html_print_page_with_header_footer (GtkHTML		     *html,
+								   GtkPrintContext 	     *context,
 			   					   gdouble		      header_height,
 								   gdouble		      footer_height,
 								   GtkHTMLPrintCallback       header_print,
 							           GtkHTMLPrintCallback       footer_print,
 								   gpointer 		      user_data);
 void			   gtk_html_print_page			   (GtkHTML		      *html,
-								    GtkPrintContext	      *print_context);
+								    GtkPrintContext	      *context);
 gint 			   gtk_html_print_page_get_pages_num 	  (GtkHTML		      *html,
-								   GtkPrintContext            *print_context,
+								   GtkPrintContext            *context,
 					 			   gdouble		       header_height,
 								   gdouble                     footer_height);
+GtkPrintOperationResult	   gtk_html_print_operation_run		  (GtkHTML                    *html,
+								   GtkPrintOperation          *operation,
+								   GtkPrintOperationAction     action,
+								   GtkWindow                  *parent,
+								   GtkHTMLPrintCalcHeight      calc_header_height,
+								   GtkHTMLPrintCalcHeight      calc_footer_height,
+								   GtkHTMLPrintDrawFunc        draw_header,
+								   GtkHTMLPrintDrawFunc        draw_footer,
+								   gpointer                    user_data,
+								   GError                    **error);
 
 /* Title.  */
 const gchar               *gtk_html_get_title                     (GtkHTML                   *html);
