wxPdfDoc-0.9.2.1 : is this a bug ? Topic is solved

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Post Reply
User avatar
xaviou
Super wx Problem Solver
Super wx Problem Solver
Posts: 437
Joined: Mon Aug 21, 2006 3:18 pm
Location: Annecy - France
Contact:

wxPdfDoc-0.9.2.1 : is this a bug ?

Post by xaviou »

Hi

I would just have a confirmation before filling a bug report : it seems there is a problem when using the "WriteXml" method in the "Footer" method of a wxPdfDocument derived class.

This can be reproduced by editing the "tutorial2" file of the "minimal" sample :

Code: Select all

  // Page footer
  void Footer()
  {
    SetFont(wxT("Helvetica"),wxT("I"),8);
    wxString sFooter=_T("<table border=\"2\" valign=\"bottom\">");
    //sFooter << _T("<colgroup><col width=\"252\"/><col width=\"25\"/></colgroup>");
    sFooter << _T("<tbody><tr><td>Last update : <b>") << wxDateTime::Now().Format(_T("%c"));
    sFooter << _T("</b> by <b>") << wxGetUserName() << _T("</b></td><td align=\"right\">");
    sFooter << wxString::Format(_("Page <b>%d</b>/<b>{nb}</b>"), PageNo()) << _T("</td></tr></tbody></table><br/>");
    WriteXml(sFooter);
  }
This simply makes the application to terminate without any error message.

And this crash does not append if there is less than one page of text to be printed (replacing the number of lines printed from 40 to 23 works; if more than 23, it crash).

Am I doing something wrong ?

Regards
Xav'
My wxWidgets stuff web page : X@v's wxStuff
User avatar
xaviou
Super wx Problem Solver
Super wx Problem Solver
Posts: 437
Joined: Mon Aug 21, 2006 3:18 pm
Location: Annecy - France
Contact:

Re: wxPdfDoc-0.9.2.1 : is this a bug ?

Post by xaviou »

After more tests, it seems to be a problem with the "table" tags.

With a simple "<p>" tag, it works fine. For example :

Code: Select all

  // Page footer
  void Footer()
  {
    SetFont(wxT("Helvetica"),wxT("I"),8);
    wxString sFooter=_T("");
    sFooter << _T("<p>Last update : <b>") << wxDateTime::Now().Format(_T("%c"));
    sFooter << _T("</b> by <b>") << wxGetUserName() << _T("</b></p>");
    SetY(-15);
    WriteXml(sFooter);
    SetY(-15);
    WriteXml(wxString::Format(_T("<p align=\"right\">Page <b>%d</b>/<b>{nb}</b></p>"), PageNo()));
  }
Regards

Xav'
My wxWidgets stuff web page : X@v's wxStuff
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxPdfDoc-0.9.2.1 : is this a bug ?

Post by utelle »

xaviou wrote:I would just have a confirmation before filling a bug report : it seems there is a problem when using the "WriteXml" method in the "Footer" method of a wxPdfDocument derived class.
The problem is related to the autopagebreak feature of wxPdfDocument. As soon as content is printed at a position below the bottom margin of a page a page break is performed. If this happens within the Footer method you get an endless loop causing a stack overflow.

Method WriteXML and especially the table tag handling code doesn't check whether the method was called within the footer. You might call this a bug, but on the other hand it's your responsibility that the footer content fits on the page.

If you want to use the bottom margin space for your footer you must temporarily disable autopagebreak within your own footer method:

Code: Select all

void YourDerivedPdfDocumentClass::Footer()
{
  double breakMargin = GetBreakMargin();
  SetAutoPageBreak(false);
  // ... (your footer content)
  SetAutoPageBreak(true, breakMargin);
]
This still doesn't solve the problem if the margin space is not big enough to hold the complete footer content.

So the code of wxPdfDocument should certainly be fixed in such a way that the application doesn't crash. I'll try to address this issue for the next release of wxPdfDocument.

But even with such a fix you would get garbled results if your footer content doesn't fit on the page.
xaviou wrote:And this crash does not append if there is less than one page of text to be printed (replacing the number of lines printed from 40 to 23 works; if more than 23, it crash).

Am I doing something wrong ?
At least it's partially your own responsibility. In the shown code of your footer method the y position is not set. So the footer is printed at that position where the last normal line was printed. If you print more lines as fit on the page (i.e. more than 23) the autopagebreak feature comes into action. Your footer method then causes page breaks itself and you get a crash due to a stack overflow.

If you take a look at the footer method in tutorial 2 you see that the y position is manipulated by a negative offset to the bottom margin. And therefore the footer in this sample is guaranteed to fit on the page without causing a page break.

It is good practice to explicitly set the y position within the footer method so that the content which is printed within the footer is known to fit on the page (taking the margins into account, too, of course).

Regards,

Ulrich
User avatar
xaviou
Super wx Problem Solver
Super wx Problem Solver
Posts: 437
Joined: Mon Aug 21, 2006 3:18 pm
Location: Annecy - France
Contact:

Re: wxPdfDoc-0.9.2.1 : is this a bug ?

Post by xaviou »

Hi
utelle wrote:If you take a look at the footer method in tutorial 2 you see that the y position is manipulated by a negative offset to the bottom margin. And therefore the footer in this sample is guaranteed to fit on the page without causing a page break.
I saw this, and the first crash I had append when I tried to use "WriteXml" after a "SetY(-20)"

With your above explanations, I now understand why it crashed (the breakmargin has a default value of 20) :oops: .

The "disabling the auto page break" tip is enough in my case.

Thanks again for your explanations.

Regards
Xav'
My wxWidgets stuff web page : X@v's wxStuff
Post Reply