Bug 144935

Summary: kpdf ignores paper size when printing
Product: [Unmaintained] kpdf Reporter: François Tigeot <ftigeot>
Component: generalAssignee: Albert Astals Cid <aacid>
Status: RESOLVED FIXED    
Severity: normal    
Priority: NOR    
Version: unspecified   
Target Milestone: ---   
Platform: NetBSD pkgsrc   
OS: Other   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description François Tigeot 2007-05-01 20:13:14 UTC
Version:            (using KDE KDE 3.5.6)
Installed from:    NetBSD pkgsrc
Compiler:          gcc 3.4.6 propolice, visibility
OS:                Other

I use a Xerox Phaser 8200N printer with two separate cups entries: a default one for A4 sized paper and an other one for DL sized envelopes.
Printing A4 pdf files with the default cups entry works fine.
When printing an envelope, the printer always asks to insert A4 sheets even though the pdfs are DL sized and the CUPS entry also specifies a "DL envelope" paper size.

Printing envelopes works fine with the same CUPS entry and kcontrol, so I'm positive this should be a kpdf specific issue.
Comment 1 Albert Astals Cid 2007-05-01 20:32:46 UTC
"When printing an envelope, the printer always asks to insert A4 sheets even though the pdfs are DL sized and the CUPS entry also specifies a "DL envelope" paper size."

CUPS entry? you mean you selected "Evolope DL" "Page Size": combo from the "General" tab in the dialog that appears when pressing "Properties" in the printing dialog that appears when using print from kpdf?

Are you able to add test patch?
Comment 2 François Tigeot 2007-05-01 20:42:04 UTC
Albert Astals Cid wrote:
[bugs.kde.org quoted mail]

Yes, that's it.

> Are you able to add test patch?


Please send, I'll test asap.
Comment 3 Albert Astals Cid 2007-05-01 21:04:24 UTC
Well, that is not an attempt to fix it, just to see if you are having the same behaviour than me

Index: core/generator_pdf/generator_pdf.cpp
===================================================================
--- core/generator_pdf/generator_pdf.cpp        (revision 660118)
+++ core/generator_pdf/generator_pdf.cpp        (working copy)
@@ -420,6 +420,7 @@
     marginBottom = (int)printer.option("kde-margin-bottom").toDouble();
     bool forceRasterize = printer.option("kde-kpdf-forceRaster").toInt();

+    KMessageBox::information(0, QString("You are printing with paper size %1").arg(ps));
     if (ps.find(QRegExp("w\\d+h\\d+")) == 0)
     {
         // size not supported by Qt, KPrinter gives us the size as wWIDTHhHEIGHT
@@ -440,6 +441,7 @@
         paperWidth = metrics.width();
         paperHeight = metrics.height();
     }
+    KMessageBox::information(0, QString("You are printing with paper size %1 %2").arg(paperWidth).arg(paperHeight));

     KTempFile tf( QString::null, ".ps" );
     globalParams->setPSPaperWidth(paperWidth);
@@ -499,7 +501,8 @@
         // needs to be here so that the file is flushed, do not merge with the one
         // in the else
         delete psOut;
-        printer.printFiles(tf.name(), true);
+        printer.printFiles(tf.name(), false);
+        KMessageBox::information(0, QString("The file we printed was left at %1 please open it with an editor and check %%DocumentMedia: and %%BoundingBox: look correct").arg(tf.name()));
         return true;
     }
     else


When printing a document with envolope DL i get

You are printing with paper size EnvDL
You are printing with paper size 312 624
and the file has
%%DocumentMedia: plain 312 624 0 () ()
%%BoundingBox: 0 0 312 624

That is the correct size in points of a Envelope DL

Do you get the same data?
Comment 4 François Tigeot 2007-05-01 21:55:17 UTC
Albert Astals Cid wrote:
[bugs.kde.org quoted mail]

[patch removed]

> When printing a document with envolope DL i get
> 
> You are printing with paper size EnvDL
> You are printing with paper size 312 624
> and the file has
> %%DocumentMedia: plain 312 624 0 () ()
> %%BoundingBox: 0 0 312 624
> 
> That is the correct size in points of a Envelope DL
> 
> Do you get the same data?


Definitely not.
I choose to print into a .ps file. The dimensions were not the same and I
even got additional message boxes:

You are printing with paper size
You are printing with paper size 595 842

Message box: "Warning - KPDF"
The file format is not directly supported by the current print system. You
now have 3 options:
* KDE can attempt to convert this file automatically to a supported
format. (Select Convert)
* You can try to send the file to the printer without any conversion.
(Select Keep)
* You can cancel the printjob. (Select Cancel)
Do you want KDE to attempt and convert this file to application/postscript?
Convert / Keep / Cancel => Keep

Message box: "Catastrophe! - KNotify"
A print error occurred. Error message received from system:
Cannot save print file to /home/ftigeot/print.ps. Check that you have
write access to it.

(The file was correctly written, it is a landscape A4 image of the
original pdf)

%%DocumentMedia: plain 595 842 0 () ()
%%BoundingBox: 0 0 595 842
Comment 5 Albert Astals Cid 2007-05-01 22:06:17 UTC
When choosing "Print to PS" in the print dialog size choosing does not work (i still to digg why i'd say it's not completely kpdf fault) you have to print to a printer. If you don't want to lose paper while testing just comment or remove the printer.printFiles(tf.name(), false); line.

By the way don't preview either as that also bypasses size check as far as i remmeber.

If you have always using "Print to PS" instead of your printer, that may be the problem.
Comment 6 François Tigeot 2007-05-01 22:25:09 UTC
Albert Astals Cid wrote:
[bugs.kde.org quoted mail]

My bad, this was just done for the previous test.
Here are the results with a real printer :

You are printing with paper size
You are printing with paper size 595 842
%%DocumentMedia: plain 595 842 0 () ()
%%BoundingBox: 0 0 595 842
Comment 7 Albert Astals Cid 2007-05-02 20:55:48 UTC
pffff, that doesn't seem good, can you add this code before the KMessageBox::information(0, QString("You are printing with paper size %1").arg(ps)); line

QString optionsText;
QMap<QString, QString>::const_iterator it = printer.options().begin();
for ( ; it != printer.options().end(); ++it ) {
    optionsText += QString("%1 %2\n").arg(it.key()).arg(it.data());
}
KMessageBox::information(0, QString("Options:\n%1").arg(optionsText));

and tell me what it gives you (i guess you already know but you can select with the mouse and copy and paste, no need to type all the text).
Comment 8 François Tigeot 2007-05-02 22:01:02 UTC
Albert Astals Cid wrote:
[bugs.kde.org quoted mail]

Options:
_kde-filters
kde-collate Collate
kde-colormode Color
kde-copies 1
kde-current 0
kde-currentpage 1
kde-isspecial 0
kde-maxpage 1
kde-minpage 1
kde-orientation Landscape
kde-outputtofile 0
kde-pageorder Forward
kde-pageset 0
kde-pagesize 26
kde-preview 0
kde-printcommand
kde-range
kde-special-command
Comment 9 Albert Astals Cid 2007-05-02 22:04:09 UTC
Just to be sure, this is when printing to a physical printer, right?
Comment 10 François Tigeot 2007-05-02 23:59:26 UTC
Albert Astals Cid wrote:
[bugs.kde.org quoted mail]

Right.
Comment 11 Albert Astals Cid 2007-05-09 20:54:27 UTC
can you try this? (you may have to revert previous patches)

Index: core/generator_pdf/generator_pdf.cpp
===================================================================
--- core/generator_pdf/generator_pdf.cpp	(revision 660118)
+++ core/generator_pdf/generator_pdf.cpp	(working copy)
@@ -411,6 +411,7 @@
 
 bool PDFGenerator::print( KPrinter& printer )
 {
+    // PageSize is a CUPS artificially created setting
     QString ps = printer.option("PageSize");
     int paperWidth, paperHeight;
     int marginTop, marginLeft, marginRight, marginBottom;
@@ -422,7 +423,7 @@
 
     if (ps.find(QRegExp("w\\d+h\\d+")) == 0)
     {
-        // size not supported by Qt, KPrinter gives us the size as wWIDTHhHEIGHT
+        // size not supported by Qt, CUPS gives us the size as wWIDTHhHEIGHT, at least on the printers i tested
         // remove the w
         ps = ps.mid(1);
         int hPos = ps.find("h");
@@ -431,10 +432,15 @@
     }
     else
     {
-        // size is supported by Qt, we get either the pageSize name or nothing because the default pageSize is used
+        // size is supported by Qt, we get either the pageSize name or nothing because the CUPS driver
+        // does not do any translation, then use KPrinter::pageSize to get the page size
+        KPrinter::PageSize qtPageSize;
+        if (!ps.isEmpty()) qtPageSize = pageNameToPageSize(ps);
+        else qtPageSize = printer.pageSize();
+
         QPrinter dummy(QPrinter::PrinterResolution);
         dummy.setFullPage(true);
-        dummy.setPageSize((QPrinter::PageSize)(ps.isEmpty() ? KGlobal::locale()->pageSize() : pageNameToPageSize(ps)));
+        dummy.setPageSize((QPrinter::PageSize)qtPageSize);
 
         QPaintDeviceMetrics metrics(&dummy);
         paperWidth = metrics.width();
Comment 12 François Tigeot 2007-05-09 23:10:28 UTC
Albert Astals Cid wrote:
> 
> can you try this? (you may have to revert previous patches)


Everything is working perfectly with the patch. I can now print envelopes
without any problem.

Thanks a lot !
Comment 13 Albert Astals Cid 2007-05-09 23:18:37 UTC
SVN commit 663027 by aacid:

Improve handling of page sizes when printing.

Thanks a lot for testing the patch

BUGS: 144935


 M  +9 -3      generator_pdf.cpp  


--- branches/KDE/3.5/kdegraphics/kpdf/core/generator_pdf/generator_pdf.cpp #663026:663027
@@ -411,6 +411,7 @@
 
 bool PDFGenerator::print( KPrinter& printer )
 {
+    // PageSize is a CUPS artificially created setting
     QString ps = printer.option("PageSize");
     int paperWidth, paperHeight;
     int marginTop, marginLeft, marginRight, marginBottom;
@@ -422,7 +423,7 @@
 
     if (ps.find(QRegExp("w\\d+h\\d+")) == 0)
     {
-        // size not supported by Qt, KPrinter gives us the size as wWIDTHhHEIGHT
+        // size not supported by Qt, CUPS gives us the size as wWIDTHhHEIGHT, at least on the printers i tester
         // remove the w
         ps = ps.mid(1);
         int hPos = ps.find("h");
@@ -431,10 +432,15 @@
     }
     else
     {
-        // size is supported by Qt, we get either the pageSize name or nothing because the default pageSize is used
+        // size is supported by Qt, we get either the pageSize name or nothing because the CUPS driver
+        // does not do any translation, then use KPrinter::pageSize to get the page size
+        KPrinter::PageSize qtPageSize;
+        if (!ps.isEmpty()) qtPageSize = pageNameToPageSize(ps);
+        else qtPageSize = printer.pageSize();
+
         QPrinter dummy(QPrinter::PrinterResolution);
         dummy.setFullPage(true);
-        dummy.setPageSize((QPrinter::PageSize)(ps.isEmpty() ? KGlobal::locale()->pageSize() : pageNameToPageSize(ps)));
+        dummy.setPageSize((QPrinter::PageSize)qtPageSize);
 
         QPaintDeviceMetrics metrics(&dummy);
         paperWidth = metrics.width();