Generar PDF desde TPrinter.

Generar PDF desde TPrinter.

Postby FiveWiDi » Sat Feb 11, 2023 12:24 pm

Hola a todos,

Estoy intentando generar un PDF de 11 páginas (irá creciendo poco a poco) desde Tprinter.

La intención es hacerlo de manera automática sin usar el oPrinter:Preview(), directamente con FWSavePreviewToPDF( oPrinter, oPrinter:cFile, .T. ), pero pierde alguna página a veces sobre todo en el primer intento (es todo texto plano).

Veo que oPrinter tiene estos valores:

oPrinter:lUseHaruPDF = .F.
IsHaruLinked() = .F.
oPrinter:lUseI2PDF = .F.
fw_i2pdf() = .F.
oPrinter:lUseFWPDF = .F.

que no sé si se deben setear manualmente (las datas claro) o instalar/lincar algo.

¿Cómo puedo hacerlo? Generar un PDF desde TPrinter sin pasar por Preview()

Muchas gracias,
Un Saludo
Carlos G.

FiveWin 24.02 + Harbour 3.2.0dev (r2403071241), BCC 7.7 Windows 10
FiveWiDi
 
Posts: 1060
Joined: Mon Oct 10, 2005 2:38 pm

Re: Generar PDF desde TPrinter.

Postby cmsoft » Sat Feb 11, 2023 12:59 pm

Mira el ejemplo en testprn6.prg en samples
Te lo dejo acá por las dudas...
Code: Select all  Expand view

// Printing an ascii file

#define REVISED // March 2022

#ifdef REVISED

#include "fivewin.ch"

REQUEST FWHARU

function Main()

   local oPrn, oFont
   local cText := MemoRead( "testprn5.prg" )

   TPrinter():lUseHaruPDF := .t.

   PRINT oPrn PREVIEW FILE "prueba.pdf"
   DEFINE FONT oFont NAME "Courier New" SIZE 0, -10 OF oPrn
   PAGE

   @ 1,1 PRINT TO oPrn TEXT cText SIZE 6,9 INCHES FONT oFont

   ENDPAGE
   ENDPRINT
   RELEASE FONT oFont

return nil

#else
// OBSOLETE.
// Not recommended way to save to PDF


#include "FiveWin.ch"
#include "harupdf.ch"
#include "directry.ch"

#define H_SIZE 592
#define V_SIZE 800

function Main()
   local cFile
   local hDir := maKedir('png')
   local hMeta
   local oPrn, cText := MemoRead( "testprn5.prg" ), n := 0
   local nRowStep, oFont

   Aeval( Directory( "png\*.png" ),{|val| FErase( "png\" + val[ F_NAME ] ) } )

   PRINTER oPrn PREVIEW

      DEFINE FONT oFont NAME "
Courier New" SIZE 0, -10 OF oPrn
      nRowStep = oPrn:nVertRes() / 40

      PAGE
         while n < 40
            oPrn:Say( n * nRowStep, 1, MemoLine( cText,, n + 1 ), oFont )
            n++
         end
      ENDPAGE

      cFile = "
png\" + cFileNoExt( ATail( oPrn:aMeta ) ) + ".png "
//      CONVERTEMF2PNG( hMeta:=GetEnhMetaFile( ATail( oPrn:aMeta ) ), V_SIZE, H_SIZE, cFile )
//      CloseEnhMetafile( hMeta )

      FW_SaveImage( MEMOREAD( ATail( oPrn:aMeta ) ), cFile )

   ENDPRINTER

   BuildPdf()
   oFont:End()

   ShellExecute( 0, "
Open", "prueba.pdf" ) //Esto solo si ademas de crearlo quieres mostrarlo

return nil

function BuildPdf()

   LOCAL pdf := HPDF_New()
   LOCAL cFileToSave := "
prueba.pdf"
   LOCAL aDir := Directory( "
png\*.png" )
   LOCAL aFile
   LOCAL page, dst, image

   HPDF_SetCompressionMode( pdf, HPDF_COMP_ALL )

   for each aFile in aDir
      page := HPDF_AddPage(pdf)

      HPDF_Page_SetWidth(page, H_SIZE)
      HPDF_Page_SetHeight(page, V_SIZE)

      dst := HPDF_Page_CreateDestination(page)
      HPDF_Destination_SetXYZ(dst, 0, HPDF_Page_GetHeight(page), 1)
      HPDF_SetOpenAction(pdf, dst)

      // Commentout the following line if you need ASCII chart by Codepages
      //Page_CodePages( pdf )
      image := HPDF_LoadPngImageFromFile(pdf, "
png\" + aFile[ F_NAME ] )

      HPDF_Page_DrawImage(page, image, 0, 0, H_SIZE, V_SIZE)

      //FErase( aFile[ F_NAME ] )
   next

   HPDF_SaveToFile( pdf, cFileToSave )

   HPDF_Free( pdf )

   Aeval( aDir,{|val| FErase( "
png\" + val[ F_NAME ] ) } )

return nil

#endif

User avatar
cmsoft
 
Posts: 1189
Joined: Wed Nov 16, 2005 9:14 pm
Location: Mercedes - Bs As. Argentina

Re: Generar PDF desde TPrinter.

Postby FiveWiDi » Sun Feb 12, 2023 12:23 am

cmsoft wrote:Mira el ejemplo en testprn6.prg en samples
Te lo dejo acá por las dudas...
Code: Select all  Expand view

// Printing an ascii file

#define REVISED // March 2022

#ifdef REVISED

#include "fivewin.ch"

REQUEST FWHARU

function Main()

   local oPrn, oFont
   local cText := MemoRead( "testprn5.prg" )

   TPrinter():lUseHaruPDF := .t.

   PRINT oPrn PREVIEW FILE "prueba.pdf"
   DEFINE FONT oFont NAME "Courier New" SIZE 0, -10 OF oPrn
   PAGE

   @ 1,1 PRINT TO oPrn TEXT cText SIZE 6,9 INCHES FONT oFont

   ENDPAGE
   ENDPRINT
   RELEASE FONT oFont

return nil

#else
// OBSOLETE.
// Not recommended way to save to PDF


#include "FiveWin.ch"
#include "harupdf.ch"
#include "directry.ch"

#define H_SIZE 592
#define V_SIZE 800

function Main()
   local cFile
   local hDir := maKedir('png')
   local hMeta
   local oPrn, cText := MemoRead( "testprn5.prg" ), n := 0
   local nRowStep, oFont

   Aeval( Directory( "png\*.png" ),{|val| FErase( "png\" + val[ F_NAME ] ) } )

   PRINTER oPrn PREVIEW

      DEFINE FONT oFont NAME "
Courier New" SIZE 0, -10 OF oPrn
      nRowStep = oPrn:nVertRes() / 40

      PAGE
         while n < 40
            oPrn:Say( n * nRowStep, 1, MemoLine( cText,, n + 1 ), oFont )
            n++
         end
      ENDPAGE

      cFile = "
png\" + cFileNoExt( ATail( oPrn:aMeta ) ) + ".png "
//      CONVERTEMF2PNG( hMeta:=GetEnhMetaFile( ATail( oPrn:aMeta ) ), V_SIZE, H_SIZE, cFile )
//      CloseEnhMetafile( hMeta )

      FW_SaveImage( MEMOREAD( ATail( oPrn:aMeta ) ), cFile )

   ENDPRINTER

   BuildPdf()
   oFont:End()

   ShellExecute( 0, "
Open", "prueba.pdf" ) //Esto solo si ademas de crearlo quieres mostrarlo

return nil

function BuildPdf()

   LOCAL pdf := HPDF_New()
   LOCAL cFileToSave := "
prueba.pdf"
   LOCAL aDir := Directory( "
png\*.png" )
   LOCAL aFile
   LOCAL page, dst, image

   HPDF_SetCompressionMode( pdf, HPDF_COMP_ALL )

   for each aFile in aDir
      page := HPDF_AddPage(pdf)

      HPDF_Page_SetWidth(page, H_SIZE)
      HPDF_Page_SetHeight(page, V_SIZE)

      dst := HPDF_Page_CreateDestination(page)
      HPDF_Destination_SetXYZ(dst, 0, HPDF_Page_GetHeight(page), 1)
      HPDF_SetOpenAction(pdf, dst)

      // Commentout the following line if you need ASCII chart by Codepages
      //Page_CodePages( pdf )
      image := HPDF_LoadPngImageFromFile(pdf, "
png\" + aFile[ F_NAME ] )

      HPDF_Page_DrawImage(page, image, 0, 0, H_SIZE, V_SIZE)

      //FErase( aFile[ F_NAME ] )
   next

   HPDF_SaveToFile( pdf, cFileToSave )

   HPDF_Free( pdf )

   Aeval( aDir,{|val| FErase( "
png\" + val[ F_NAME ] ) } )

return nil

#endif



Perfecto, funcionando.
Muchas gracias
Un Saludo
Carlos G.

FiveWin 24.02 + Harbour 3.2.0dev (r2403071241), BCC 7.7 Windows 10
FiveWiDi
 
Posts: 1060
Joined: Mon Oct 10, 2005 2:38 pm


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 77 guests