Harupdf y acentos ( Solucionado )

Harupdf y acentos ( Solucionado )

Postby horacio » Tue Jul 02, 2019 1:33 pm

Colegas, estoy trabajando con la librería harupdf de las contribuciones de Harbour, tengo problemas con las vocales acentuadas y la ñ. Alguien resolvió? Desde ya muchas gracias.

Saludos
Last edited by horacio on Thu Jul 11, 2019 5:51 pm, edited 1 time in total.
horacio
 
Posts: 1363
Joined: Wed Jun 21, 2006 12:39 am
Location: Capital Federal Argentina

Re: Harupdf y acentos

Postby horacio » Fri Jul 05, 2019 1:18 pm

+1
horacio
 
Posts: 1363
Joined: Wed Jun 21, 2006 12:39 am
Location: Capital Federal Argentina

Re: Harupdf y acentos

Postby karinha » Fri Jul 05, 2019 1:40 pm

Horacio, muestra un ejemplo simple de lo que necesitas. De esa manera se vuelve mucho más fácil.

Hiciste eso?

Code: Select all  Expand view

#include "FiveWin.ch"
#include "hbclass.ch"
#include "harupdf.ch"
#include "hbzebra.ch"
 


Saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7794
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: Harupdf y acentos

Postby karinha » Fri Jul 05, 2019 2:08 pm

Horacio, muestra algo asi, ok? Saludos.

Code: Select all  Expand view

#include "FiveWin.ch"
#include "hbclass.ch"
#include "harupdf.ch"
#include "hbzebra.ch"
// #Include "hbhpdf.ch"  // ??

#define PDF_PORTRAIT  1
#define PDF_LANDSCAPE 2
#define PDF_TXT       3

FUNCTION Main()

   SET DELE ON
   SET CENT ON
   SET SOFT OFF
   SET DATE BRIT
   SET EPOC TO 1964
   SET EXCLUSIVE OFF

   REQUEST HB_CODEPAGE_UTF8EX // ETC

   hb_cdpSelect( "UTF8EX" )

   REQUEST DBFCDX
   REQUEST DESCEND
   RDDSETDEFAULT( "DBFCDX" )
   DBSETDRIVER( "DBFCDX" )

   // DEFINE WINDOW...

   // ACTIVATE WINDOW...

RETURN NIL

FUNCTION Testa_PDF()

   LOCAL x, cMsg, oPDF, nLinha, nLenItem, nLenDescri, nLenUnit, nLenDescont, nLenTotal, nMaxCol, nMaxRow

   oPDF := PDFClass()
   oPDF:Begin()
   oPDF:SetType( 2 )
   oPDF:SetInfo( 'Horacio', 'Sistema', 'Teste PDF' )
   oPDF:AddPage()

   nMaxCol := oPDF:MaxCol()
   nMaxRow := oPDF:MaxRow()

   oPDF:DrawText( 2.0, 42, PadC( "Cartório do Ofício Único de Deus Me Livre", nMaxCol - 80 ), Nil, 10, "Courier", Nil, Nil )
   oPDF:DrawText( 3.5, 42, PadC( "CEP 21069-069 - DML-RJ", nMaxCol - 82 ), Nil, 10, "Courier", Nil, Nil )
   oPDF:DrawText( 5.5, 42, PadC( "Tabelião: Joaquim José da Silva Xavier", nMaxCol - 95 ), Nil, 11, "Courier-Bold", Nil, Nil )
   oPDF:DrawText( 7.5, 42, PadC( "www.fivewin.com.br", nMaxCol - 80 ), Nil, 10, "Courier", Nil, Nil )

   oPDF:End()

RETURN NIL

CREATE CLASS PDFClass

   VAR    oPdf
   VAR    oPage
   VAR    cFileName         INIT ""
   VAR    nRow              INIT 999
   VAR    nCol              INIT 0
   VAR    nAngle            INIT 0
   VAR    cFontName         INIT "Courier"
   VAR    nFontSize         INIT 9
   VAR    nLineHeight       INIT 1.3
   VAR    nMargin           INIT 30
   VAR    nType             INIT 1
   VAR    nPdfPage          INIT 0
   VAR    nPageNumber       INIT 0
   VAR    cHeader           INIT {}
   VAR    cCodePage         INIT "CP1252" // "WinAnsiEncoding" // "UTF-8" // "StandardEncoding" // "FontSpecific" //

   METHOD AddPage()
   METHOD RowToPDFRow( nRow )
   METHOD ColToPDFCol( nCol )
   METHOD MaxRow()
   METHOD MaxCol()
   METHOD DrawText( nRow, nCol, xValue, cPicture, nFontSize, cFontName, nAngle, anRGB, lBold )
   METHOD DrawLine( nRowi, nColi, nRowf, nColf, nPenSize )
   METHOD DrawRetangle( nTop, nLeft, nWidth, nHeight, nPenSize, nFillType, anRGB )
   METHOD DrawImage( cJPEGFile, nRow, nCol, nWidth, nHeight )
   METHOD Cancel()
   METHOD PrnToPdf( cInputFile )
   METHOD SetType( nType )
   METHOD PageHeader()
   METHOD MaxRowTest( nRows )
   METHOD SetInfo( cAuthor, cCreator, cTitle, cSubject )
   METHOD BEGIN()
   METHOD End()

ENDCLASS

METHOD BEGIN() CLASS PDFClass

   IF ::nType > 2
      IF Empty( ::cFileName )
         ::cFileName := MyTempFile( "LST" )
      ENDIF
      SET PRINTER TO ( ::cFileName )
      SET DEVICE TO PRINT
   ELSE
      IF Empty( ::cFileName )
         ::cFileName := MyTempFile( "PDF" )
      ENDIF
      ::oPdf := HPDF_New()
      HPDF_SetCompressionMode( ::oPdf, HPDF_COMP_ALL )
      IF ::cCodePage != NIL
         HPDF_UseUTFEncodings( ::oPDF ) // mr
         //HPDF_SetCurrentEncoder( ::oPDF, ::cCodePage ) // isolado para teste / mr.
         //HPDF_Encoder_GetUnicode(::cCodePage, "UTF-8") // mr.
      ENDIF
   ENDIF

   RETURN NIL

METHOD End() CLASS PDFClass

   IF ::nType > 2
      SET DEVICE TO SCREEN
      SET PRINTER TO
      //RUN ( "cmd /c start notepad.exe " + ::cFileName )
      Hb_Run( "start /s notepad.exe " + ::cFileName )
   ELSE
      //IF ::nPdfPage == 0
      //::AddPage()
      //::DrawText( 10, 10, "NENHUM CONTEUDO (NO CONTENT)",, ::nFontSize * 2 )
      //ENDIF
      IF File( ::cFileName )
         fErase( ::cFileName )
      ENDIF

      // Set PDF/A-1b conformance //
      //msginfo('conversão pdf/a')
      HPDF_PDFA_SetPDFAConformance( ::oPdf, HPDF_PDFA_1B )

      HPDF_SaveToFile( ::oPdf, ::cFileName )
      HPDF_Free( ::oPdf )
      RUN ( "cmd /c start " + ::cFileName )
   ENDIF

   RETURN NIL

METHOD SetInfo( cAuthor, cCreator, cTitle, cSubject ) CLASS PDFClass

   IF ::nType > 2
      RETURN NIL
   ENDIF
   cAuthor  := iif( cAuthor == NIL, "JPA Tecnologia", cAuthor )
   cCreator := iif( cCreator == NIL, "Harupdf", cCreator )
   cTitle   := iif( cTitle == NIL, "", cTitle )
   cSubject := iif( cSubject == NIL, cTitle, cSubject )
   HPDF_SetInfoAttr( ::oPDF, HPDF_INFO_AUTHOR, cAuthor )
   HPDF_SetInfoAttr( ::oPDF, HPDF_INFO_CREATOR, cCreator )
   HPDF_SetInfoAttr( ::oPDF, HPDF_INFO_TITLE, cTitle )
   HPDF_SetInfoAttr( ::oPdf, HPDF_INFO_SUBJECT, cSubject )
//HPDF_SetInfoDateAttr( Year( Date() ), Month( Date() ), Day( Date() ), Val( Substr( Time(), 1, 2 ) ), Val( Substr( Time(), 4, 2 ) ), Val( Substr( Time(), 7, 2 ) ), "+", 4, 0 )

   RETURN NIL

METHOD SetType( nType ) CLASS PDFClass

   IF nType != NIL
      ::nType := nType
   ENDIF
   ::nFontSize := iif( ::nType == 1, 9, 6 )

   RETURN NIL

METHOD AddPage() CLASS PDFClass

   LOCAL Nom_font := "", Def_font := "" // mr

   IF ::nType < 3
      ::oPage := HPDF_AddPage( ::oPdf )
      //
      /* Configura fonte externa para trabalhar com Unicode. - mr */
      Nom_font = HPDF_LoadTTFontFromFile( ::oPdf, "cour.ttf", HPDF_TRUE )
      msginfo( nom_font )
      Def_font = HPDF_GetFont( ::oPdf, Nom_font, "UTF-8" )
      msginfo( def_font )
      //
      HPDF_Page_SetSize( ::oPage, HPDF_PAGE_SIZE_A4, iif( ::nType == 2, HPDF_PAGE_PORTRAIT, HPDF_PAGE_LANDSCAPE ) )
      //HPDF_Page_SetFontAndSize( ::oPage, HPDF_GetFont( ::oPdf, ::cFontName, ::cCodePage ), ::nFontSize ) // isolado, mr.
      HPDF_Page_SetFontAndSize( ::oPage, Def_font, ::nFontSize ) // para pegar fonte externa / mr
   ENDIF
   ::nRow := 0

   RETURN NIL

METHOD Cancel() CLASS PDFClass

   IF ::nType < 3
      HPDF_Free( ::oPdf )
   ENDIF

   RETURN NIL

METHOD DrawText( nRow, nCol, xValue, cPicture, nFontSize, cFontName, nAngle, anRGB, lBold ) CLASS PDFClass

   LOCAL nRadian , cTexto
   LOCAL Nom_font := "", Def_font := "" // mr

   nFontSize := iif( nFontSize == NIL, ::nFontSize, nFontSize )
   cFontName := iif( cFontName == NIL, ::cFontName, cFontName )
   cPicture  := iif( cPicture == NIL, "", cPicture )
   nAngle    := iif( nAngle == NIL, ::nAngle, nAngle )
   cTexto    := Transform( xValue, cPicture )
   ::nCol := nCol + Len( cTexto )
   IF ::nType > 2
      @ nRow, nCol SAY cTexto
   ELSE
      //
      /* Configura fonte externa para trabalhar com Unicode. - mr */
      Nom_font = HPDF_LoadTTFontFromFile( ::oPdf, "cour.ttf", HPDF_TRUE )
      msginfo( nom_font )
      Def_font = HPDF_GetFont( ::oPdf, Nom_font, "UTF-8" )
      msginfo( def_font )
      //
      nRow := ::RowToPDFRow( nRow )
      nCol := ::ColToPDFCol( nCol )
      //HPDF_Page_SetFontAndSize( ::oPage, HPDF_GetFont( ::oPdf, cFontName, ::cCodePage ), nFontSize ) - isolado, mr.
      HPDF_Page_SetFontAndSize( ::oPage, Def_font, nFontSize ) // para pegar fonte externa / mr
      IF anRGB != NIL
         HPDF_Page_SetRGBFill( ::Page, anRGB[ 1 ], anRGB[ 2 ], anRGB[ 3 ] )
         HPDF_Page_SetRGBStroke( ::Page, anRGB[ 1 ], anRGB[ 2], anRGB[ 3] )
      ENDIF
      HPDF_Page_BeginText( ::oPage )
      //HPDF_Encoder_GetUnicode(::cCodePage, "UTF-8") // mr.
      nRadian := ( nAngle / 180 ) * 3.141592
      HPDF_Page_SetTextMatrix( ::oPage, Cos( nRadian ), Sin( nRadian ), - Sin( nRadian ), Cos( nRadian ), nCol, nRow )
      HPDF_Page_ShowText( ::oPage, cTexto )

      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      IF !EMPTY( lBold ) .AND. lBold
         HPDF_Page_SetTextMatrix( ::oPage, Cos( nRadian ), Sin( nRadian ), - Sin( nRadian ), Cos( nRadian ), ( nCol + .5 ), nRow )
         HPDF_Page_ShowText( ::oPage, cTexto )
      ENDIF
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

      HPDF_Page_EndText( ::oPage )
      IF anRGB != NIL
         HPDF_Page_SetRGBFill( ::Page, 0, 0, 0 )
         HPDF_Page_SetRGBStroke( ::Page, 0, 0, 0 )
      ENDIF
   ENDIF

   RETURN NIL

METHOD DrawLine( nRowi, nColi, nRowf, nColf, nPenSize ) CLASS PDFClass

   IF ::nType > 2
      nRowi := Round( nRowi, 0 )
      nColi := Round( nColi, 0 )
      @ nRowi, nColi SAY Replicate( "-", nColf - nColi )
      ::nCol := Col()
   ELSE
      nPenSize := iif( nPenSize == NIL, 0.2, nPenSize )
      nRowi := ::RowToPDFRow( nRowi )
      nColi := ::ColToPDFCol( nColi )
      nRowf := ::RowToPDFRow( nRowf )
      nColf := ::ColToPDFCol( nColf )
      HPDF_Page_SetLineWidth( ::oPage, nPenSize )
      HPDF_Page_MoveTo( ::oPage, nColi, nRowi )
      HPDF_Page_LineTo( ::oPage, nColf, nRowf )
      HPDF_Page_Stroke( ::oPage )
   ENDIF

   RETURN NIL

METHOD DrawImage( cJPEGFile, nRow, nCol, nWidth, nHeight ) CLASS PDFClass

   LOCAL oImage

   IF ::nType > 2
      RETURN NIL
   ENDIF
   nRow    := ::RowToPDFRow( nRow )
   nCol    := ::ColToPDFCol( nCol )
   nWidth  := Int( nWidth * ::nFontSize / 2 )
   nHeight := nHeight * ::nFontSize
   oImage := HPDF_LoadJPEGImageFromFile( ::oPdf, cJPEGFile )
   HPDF_Page_DrawImage( ::oPage, oImage, nCol, nRow, nWidth, nHeight )

   RETURN NIL

METHOD DrawRetangle( nTop, nLeft, nWidth, nHeight, nPenSize, nFillType, anRGB ) CLASS PDFClass

   IF ::nType > 2
      RETURN NIL
   ENDIF
   nFillType := iif( nFillType == NIL, 1, nFillType )
   nPenSize  := iif( nPenSize == NIL, 0.2, nPenSize )
   nTop      := ::RowToPDFRow( nTop )
   nLeft     := ::ColToPDFCol( nLeft )
   nWidth    := ( nWidth ) * ::nFontSize / 1.666
   nHeight   := - ( nHeight ) * :: nFontSize
   HPDF_Page_SetLineWidth( ::oPage, nPenSize )
   IF anRGB != NIL
      HPDF_Page_SetRGBFill( ::oPage, anRGB[ 1 ], anRGB[ 2 ], anRGB[ 3 ] )
      HPDF_Page_SetRGBStroke( ::oPage, anRGB[ 1 ], anRGB[ 2 ], anRGB[ 3 ] )
   ENDIF
   HPDF_Page_Rectangle( ::oPage, nLeft, nTop, nWidth, nHeight )
   IF nFillType == 1
      HPDF_Page_Stroke( ::oPage )     // borders only
   ELSEIF nFillType == 2
      HPDF_Page_Fill( ::oPage )       // inside only
   ELSE
      HPDF_Page_FillStroke( ::oPage ) // all
   ENDIF
   IF anRGB != NIL
      HPDF_Page_SetRGBStroke( ::oPage, 0, 0, 0 )
      HPDF_Page_SetRGBFill( ::oPage, 0, 0, 0 )
   ENDIF

   RETURN NIL

METHOD RowToPDFRow( nRow ) CLASS PDFClass

   RETURN HPDF_Page_GetHeight( ::oPage ) - ::nMargin - ( nRow * ::nFontSize * ::nLineHeight )

METHOD ColToPDFCol( nCol ) CLASS PDFClass

   RETURN nCol * ::nFontSize / 1.666 + ::nMargin

METHOD MaxRow() CLASS PDFClass

   LOCAL nPageHeight, nMaxRow

   IF ::nType > 2
      RETURN 63
   ENDIF
   nPageHeight := HPDF_Page_GetHeight( ::oPage ) - ( ::nMargin * 2 )
   nMaxRow     := Int( nPageHeight / ( ::nFontSize * ::nLineHeight )  )

   RETURN nMaxRow

METHOD MaxCol() CLASS PDFClass

   LOCAL nPageWidth, nMaxCol

   IF ::nType > 2
      RETURN 132
   ENDIF
   nPageWidth := HPDF_Page_GetWidth( ::oPage ) - ( ::nMargin * 2 )
   nMaxCol    := Int( nPageWidth / ::nFontSize * 1.666 )

   RETURN nMaxCol

METHOD PrnToPdf( cInputFile ) CLASS PDFClass

   LOCAL cTxtReport, cTxtPage, cTxtLine, nRow

   cTxtReport := MemoRead( cInputFile ) + Chr( 12 )
   TokenInit( @cTxtReport, Chr( 12 ) )
   DO WHILE ! TokenEnd()
      cTxtPage := TokenNEXT( cTxtReport ) + HB_EOL()
      IF Len( cTxtPage ) > 5
         IF Substr( cTxtPage, 1, 1 ) == Chr( 13 )
            cTxtPage := Substr( cTxtPage, 2 )
         ENDIF
         ::AddPage()
         nRow := 0
         DO WHILE At( HB_EOL(), cTxtPage ) != 0
            cTxtLine := Substr( cTxtPage, 1, At( HB_EOL(), cTxtPage ) - 1 )
            cTxtPage := Substr( cTxtPage, At( HB_EOL(), cTxtPage ) + 2 )
            ::DrawText( nRow++, 0, cTxtLine )
         ENDDO
      ENDIF
   ENDDO

   RETURN NIL

METHOD PageHeader() CLASS PDFClass

   ::nPdfPage    += 1
   ::nPageNumber += 1
   ::nRow        := 0
   ::AddPage()
   ::DrawText( 0, 0, "EMPRESA DE TESTE" )
   ::DrawText( 0, ( ::MaxCol() - Len( ::cHeader ) ) / 2, ::cHeader )
   ::DrawText( 0, ::MaxCol() - 12, "Page " + StrZero( ::nPageNumber, 6 ) )
   ::DrawLine( 0.5, 0, 0.5, ::MaxCol() )
   ::nRow := 2
   ::nCol := 0

   RETURN NIL

METHOD MaxRowTest( nRows ) CLASS PDFClass

   nRows := iif( nRows == NIL, 0, nRows )
   IF ::nRow > ::MaxRow() - 2 - nRows
      ::PageHeader()
   ENDIF

   RETURN NIL
 
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7794
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: Harupdf y acentos

Postby horacio » Thu Jul 11, 2019 5:50 pm

Gracias Joao por tu tiempo. Ya pude solucionarlo. La magia está en la selección de la fuente.

Code: Select all  Expand view

font := HPDF_GetFont( pdf, "Helvetica" , Nil ) //no muestra los caracteres especiales
font := HPDF_GetFont( pdf, "Helvetica", "WinAnsiEncoding" ) //con este encodig funciona perfecto
 


Saludos
horacio
 
Posts: 1363
Joined: Wed Jun 21, 2006 12:39 am
Location: Capital Federal Argentina


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 45 guests