multi page PDF

Added by Ron Tidwell 2 months ago

Hi All,

I am new at using WT. I have got most of the things working. I am working on printing and I need to create a multi-page pdf file.

Is there a way to create a multi-page pdf file? I am not talking about xhtml to pdf that Render seems to do(from the documentation it looks it only does the whole HTML page). What I am trying to do is printing some of data. This data is from a selection box that is on the page they are looking. I do not want the whole HTML page. This document can be 1 page to 100 pages. I can create a single page pdf no problem using Wt::WPdfImage. How can I make multi-page document. I can easily create pages 1 by 1 if need to, but how to get it into multi-page pdf format I do not know how to do this.

Any Help would be greatly appreciated.

Thanks,
Ron


Replies (2)

RE: multi page PDF - Added by Roel Standaert 2 months ago

You can use WPDFImage's second constructor for this.

You'll have to create a HPDF_Doc with HPDF_New, create new pages with HPDF_AddPage, and create a WPdfImage instance for every page you add.

Here's a little example that creates three pages and saves the document to a file:

#include <Wt/WPainter>
#include <Wt/WPdfImage>

#include <hpdf.h>

namespace {
  void HPDF_STDCALL error_handler(HPDF_STATUS error_no, HPDF_STATUS detail_no, void *)
  {
    fprintf(stderr, "libharu error: error_no=%04X, detail_no=%d\n",
                          (unsigned int) error_no, (int) detail_no);
  }
}

int main()
{
  HPDF_Doc pdf = HPDF_New(error_handler, 0);

  HPDF_UseUTFEncodings(pdf);

  for (int i = 0; i < 3; ++i) {
    HPDF_Page page = HPDF_AddPage(pdf);
    HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);

    HPDF_REAL width = HPDF_Page_GetWidth(page);
    HPDF_REAL height = HPDF_Page_GetHeight(page);

    Wt::WPdfImage image(pdf, page, 0, 0, width, height);
    Wt::WPainter painter(&image);
    painter.drawText(0, 0, width, height,
                     Wt::AlignCenter | Wt::AlignMiddle,
                     Wt::TextSingleLine,
                     Wt::utf8("Hello! {1}").arg(i));
  }

  HPDF_SaveToFile(pdf, "/tmp/test.pdf");

  HPDF_Free(pdf);
}

Regards,
Roel

RE: multi page PDF - Added by Ron Tidwell 2 months ago

Thank you very much!!!

Ron

(1-2/2)