Bug 29861

Summary: [Qt] Webkit print using the screen media type + Patch
Product: WebKit Reporter: Tor Arne Vestbø <vestbo>
Component: PrintingAssignee: QtWebKit Unassigned <webkit-qt-unassigned>
Status: RESOLVED DUPLICATE    
Severity: Normal CC: hausmann, h.goebel, kenneth, kent.hansen
Priority: P2 Keywords: Qt
Version: 528+ (Nightly build)   
Hardware: Other   
OS: OS X 10.5   

Description Tor Arne Vestbø 2009-09-29 05:45:53 PDT
This bug report originated from issue QTBUG-3894
http://bugreports.qt.nokia.com/browse/QTBUG-3894

--- Description ---

At some times when, printing a page in webkit, especially to a virtual printer,  you really want to print using the screen media type, and not the print media type after all. This patches adds support for this, in a not as clean as could be desired way.

Patch:

diff -ur qt-x11-opensource-src-4.5.0-old/src/3rdparty/webkit/WebCore/page/Frame.cpp qt-x11-opensource-src-4.5.0/src/3rdparty/webkit/WebCore/page/Frame.cpp
--- qt-x11-opensource-src-4.5.0-old/src/3rdparty/webkit/WebCore/page/Frame.cpp 2009-02-25 22:09:20.000000000 +0100
+++ qt-x11-opensource-src-4.5.0/src/3rdparty/webkit/WebCore/page/Frame.cpp 2009-03-14 10:11:45.000000000 +0100
@@ -726,13 +726,13 @@
         view()->layout();
 }
 
-void Frame::setPrinting(bool printing, float minPageWidth, float maxPageWidth, bool adjustViewSize)
+void Frame::setPrinting(bool printing, float minPageWidth, float maxPageWidth, bool adjustViewSize, bool usePrintMediaType)
 {
     if (!d->m_doc)
         return;
 
     d->m_doc->setPrinting(printing);
-    view()->setMediaType(printing ? "print" : "screen");
+    view()->setMediaType((printing && usePrintMediaType) ? "print" : "screen");
     d->m_doc->updateStyleSelector();
     forceLayoutWithPageWidthRange(minPageWidth, maxPageWidth, adjustViewSize);
 
diff -ur qt-x11-opensource-src-4.5.0-old/src/3rdparty/webkit/WebCore/page/Frame.h qt-x11-opensource-src-4.5.0/src/3rdparty/webkit/WebCore/page/Frame.h
--- qt-x11-opensource-src-4.5.0-old/src/3rdparty/webkit/WebCore/page/Frame.h 2009-02-25 22:09:20.000000000 +0100
+++ qt-x11-opensource-src-4.5.0/src/3rdparty/webkit/WebCore/page/Frame.h 2009-03-14 10:11:52.000000000 +0100
@@ -125,7 +125,7 @@
     void setUserStyleSheet(const String& styleSheetData);
 #endif
 
-    void setPrinting(bool printing, float minPageWidth, float maxPageWidth, bool adjustViewSize);
+    void setPrinting(bool printing, float minPageWidth, float maxPageWidth, bool adjustViewSize, bool usePrintMediaType=true);
 
     bool inViewSourceMode() const;
     void setInViewSourceMode(bool = true) const;
diff -ur qt-x11-opensource-src-4.5.0-old/src/3rdparty/webkit/WebCore/page/PrintContext.cpp qt-x11-opensource-src-4.5.0/src/3rdparty/webkit/WebCore/page/PrintContext.cpp
--- qt-x11-opensource-src-4.5.0-old/src/3rdparty/webkit/WebCore/page/PrintContext.cpp 2009-02-25 22:09:20.000000000 +0100
+++ qt-x11-opensource-src-4.5.0/src/3rdparty/webkit/WebCore/page/PrintContext.cpp 2009-03-14 10:12:43.000000000 +0100
@@ -93,7 +93,7 @@
     } while (printedPagesHeight < docHeight);
 }
 
-void PrintContext::begin(float width)
+void PrintContext::begin(float width, bool usePrintMediaType)
 {
     // By imaging to a width a little wider than the available pixels,
     // thin pages will be scaled down a little, matching the way they
@@ -113,7 +113,7 @@
 
     // FIXME: This will modify the rendering of the on-screen frame.
     // Could lead to flicker during printing.
-    m_frame->setPrinting(true, minLayoutWidth, maxLayoutWidth, true);
+    m_frame->setPrinting(true, minLayoutWidth, maxLayoutWidth, true, usePrintMediaType);
 }
 
 void PrintContext::spoolPage(GraphicsContext& ctx, int pageNumber, float width)
diff -ur qt-x11-opensource-src-4.5.0-old/src/3rdparty/webkit/WebCore/page/PrintContext.h qt-x11-opensource-src-4.5.0/src/3rdparty/webkit/WebCore/page/PrintContext.h
--- qt-x11-opensource-src-4.5.0-old/src/3rdparty/webkit/WebCore/page/PrintContext.h 2009-02-25 22:09:20.000000000 +0100
+++ qt-x11-opensource-src-4.5.0/src/3rdparty/webkit/WebCore/page/PrintContext.h 2009-03-14 10:12:19.000000000 +0100
@@ -40,7 +40,7 @@
     void computePageRects(const FloatRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, float& outPageHeight);
 
     // TODO: eliminate width param
-    void begin(float width);
+    void begin(float width, bool usePrintMediaType=true);
 
     // TODO: eliminate width param
     void spoolPage(GraphicsContext& ctx, int pageNumber, float width);
Comment 1 Kent Hansen 2010-03-16 05:08:28 PDT
OK, need to decide if we want to add this enhancement.
Comment 2 Simon Hausmann 2010-03-18 15:46:21 PDT
Kenneth, I recall that you were recently talking about a similar patch for setting the media type. Is this a duplicate bug?
Comment 3 Kenneth Rohde Christiansen 2010-03-19 10:48:34 PDT
(In reply to comment #2)
> Kenneth, I recall that you were recently talking about a similar patch for
> setting the media type. Is this a duplicate bug?

Why would you want to print with disaccordance to the spec? When printing you are supposed to use CSS according to the "print" media type.

I don't understand the use case.
Comment 4 h.goebel 2010-05-14 02:43:21 PDT
This is very useful for making a "screenshot" of the web-page into a PDF. In this case you do not want to "print", but get the same result as displayed on screen.

This is a quite common case for documentation purposes, marketing, etc. The web is full of people looking for such a solution. There are quite a few add-ons for Firefox promising this -- but all of them are using some online-service.

Having such a feature in Webkit would be a great benefit.
Comment 5 Kenneth Rohde Christiansen 2010-05-14 05:30:18 PDT
(In reply to comment #4)
> This is very useful for making a "screenshot" of the web-page into a PDF. In this case you do not want to "print", but get the same result as displayed on screen.
> 
> This is a quite common case for documentation purposes, marketing, etc. The web is full of people looking for such a solution. There are quite a few add-ons for Firefox promising this -- but all of them are using some online-service.
> 
> Having such a feature in Webkit would be a great benefit.

You do not need to print to make a screenshot, you an just paint to an image. Print might do more that what you want as it layouts with a given width.
Comment 6 h.goebel 2010-05-14 09:48:23 PDT
Can you please point me so a example code painting to a PDF file then? Thanks a lot!
Comment 7 Alexey Proskuryakov 2010-06-12 15:39:07 PDT

*** This bug has been marked as a duplicate of bug 25866 ***