Project

General

Profile

Bug #3365

WPdfRenderer and UTF-8

Added by Michael Shestero about 8 years ago. Updated almost 8 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Target version:
Start date:
06/21/2014
Due date:
% Done:

0%

Estimated time:

Description

My task is to create PDF with tables and I try to use WPdfRenderer.

I cannot make WPdfRenderer to render UTF-8 characters.

I use libharu-RELEASE_2_3_0RC2 sources patcher with truetype_utf8.patch from here:

https://groups.google.com/forum/#!msg/libharu/YzXoH_K3OAI/hxinqnq5Um0J

The libharu itself do output UTF-8 characters. But WPdfRenderer don't!

Please help!

See my sample program below:

#include <locale.h>
//#include <boost/bind.hpp>

#include <iostream>

#include <Wt/Render/WPdfRenderer>
#include <hpdf.h>

using namespace Wt;

extern "C" {
  HPDF_STATUS HPDF_UseUTFEncodings(HPDF_Doc pdf);
}

void error_handler(HPDF_STATUS   error_no,
           HPDF_STATUS   detail_no,
                   void         *user_data)
{
  std::cerr << "libharu error: error_no="
            << (unsigned int) error_no
            << ", detail_no="
            << (int) detail_no
            << std::endl;
}

void testTwo()
{
  HPDF_Doc pdf = HPDF_New(error_handler, 0);

  HPDF_SetCompressionMode(pdf, HPDF_COMP_ALL);

  // Note: UTF-8 encoding (for TrueType fonts) is only available since libharu 2.3.0 !
  HPDF_UseUTFEncodings(pdf); // will register encoder "UTF-8"
  HPDF_SetCurrentEncoder(pdf, "UTF-8");  // hpdf_encoder_utf.c

  const char* font_name =
          // HPDF_LoadTTFontFromFile(pdf, "/usr/local/fonts/arial.ttf", HPDF_TRUE);
          // HPDF_LoadTTFontFromFile(pdf, "/usr/share/fonts/truetype/freefont/FreeSans.ttf", HPDF_TRUE);
          HPDF_LoadTTFontFromFile(pdf, "/usr/share/fonts/truetype/freefont/FreeMono.ttf", HPDF_TRUE);

  std::cout << "Font name: " << font_name << std::endl;
  HPDF_Font font = HPDF_GetFont(pdf, font_name, "UTF-8");


  HPDF_Page page = HPDF_AddPage(pdf);
  HPDF_Page_SetFontAndSize(page, font, 15.0);

  HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_LANDSCAPE);


  HPDF_Page_BeginText (page);
  HPDF_Page_TextRect (page, 80.0,80.0,530,430,
                            "üöäÄÜÖß AND русский", //  ok!
                            HPDF_TALIGN_LEFT, NULL);
  HPDF_Page_EndText (page);

  Render::WPdfRenderer renderer(pdf, page);

  // Only needed if Wt is not linked against libpango
  renderer.addFontCollection("/usr/share/fonts/truetype");

  renderer.setMargin(2.54);
  renderer.setDpi(96);
  renderer.setFontScale(1);

  renderer.render(
            "<p>Copiright mark: &copy;</p><p>Russian text here: <b>проверка</b></p>",
          0 );
  // copyright mark also don't render proprely  :-(

  HPDF_SaveToFile(pdf, "xhtml2pdf.pdf" );
  HPDF_Free(pdf);
}

int main(int argc, char **argv)
{
  // Installed locales: locale -a
  //setlocale (LC_ALL,"ru_RU.utf8");
  //std::locale::global( std::locale("ru_RU.utf8") );
  std::cout << "C   Locale is: " << setlocale(LC_ALL,NULL) << std::endl; // ok
  std::cout << "C++ Locale is: " << std::locale().name() << std::endl; // ok

  testTwo();
}

Files

xhtml2pdf.pdf (196 KB) xhtml2pdf.pdf Koen Deforche, 06/23/2014 11:56 AM
xhtml2pdf.pdf (196 KB) xhtml2pdf.pdf Koen Deforche, 06/23/2014 04:07 PM
u.pdf (200 KB) u.pdf Michael Shestero, 07/07/2014 02:32 PM
u.html (16.3 KB) u.html Michael Shestero, 10/06/2014 07:46 PM

Also available in: Atom PDF