FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour CONVERTIR html A pdf
Posts: 883
Joined: Tue Oct 11, 2005 11:57 AM
CONVERTIR html A pdf
Posted: Thu Dec 18, 2025 01:52 PM

Saludos fivewinners.

Necesito en una aplicación para poder convertir desde adentro de mi sistema algunos archivos HTML a PDF El usuario los selecciona y los debe convertir en PDF dentro de una directorio que el sistema les entrega. Conocen algún convertidor en modo consola que yo pueda llamarlo desde adentro del sistema y que me haga el trabajo. Obviamente por control interno no puedo utilizar un convertidor en linea.

Cualquier ayuda sirve. Saludos desde Chile

;-) Ji,ji,ji... buena la cosa... "all you need is code"

http://www.xdata.cl - Desarrollo Inteligente
----------
Asus TUF F15, 32GB Ram, 2 * 1 TB NVME M.2, GTX 1650
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: CONVERTIR html A pdf
Posted: Thu Dec 18, 2025 03:40 PM
Regards,



Hakan ONEMLI



Harbour & MSVC 2022 & FWH 23.06
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: CONVERTIR html A pdf
Posted: Thu Dec 18, 2025 05:48 PM

USE: wkhtmltopdf.exe

// C:\FWH\SAMPLES\HTML2PDF.PRG - kapiabafwh@gmail.com - https://wkhtmltopdf.org/
// USE: wkhtmltopdf.exe

#include 'fivewin.ch'

FUNCTION Main()

   LOCAL cHtmlFile, cPdfFile, cWkHtmlToPdfPath, cCommand

   cHtmlFile := "C:\TEMP\CHAT.html"  // origem  -> ex: C\TEMP
   cPdfFile  := ".\OUTPUT.pdf"       // destino -> ex: C:\FWH\SAMPLES

   IF FILE( cPdfFile )

    FErase( cPdfFile )

   ENDIF

   IF FILE( cPdfFile )

     ? [AQUI, PROBLEMAS EN EL *.PDF]

     QUIT

     RETURN NIL

ENDIF

   // https://wkhtmltopdf.org/ ->  o de otro sitio mejor. a Tu gosto personal.
   // C:\wkhtml\bin  -> mi ejemplo en testes perfectos.
   // cWkHtmlToPdfPath := "C:\Path\To\wkhtmltopdf.exe" // Adjust path as necessary
   cWkHtmlToPdfPath := "C:\wkhtml\bin\wkhtmltopdf.exe"

   // Command to convert the HTML file to a PDF file
   cCommand := cWkHtmlToPdfPath + " " + cHtmlFile + " " + cPdfFile

   // Execute the command (uses 'RUN' for simplicity; other functions like HB_RUN might be better for capturing output/errors)
   // RUN ( cCommand )
   MYRUN ( cCommand )

   // Check if the file was created and handle errors
   IF FILE( cPdfFile ) // Si ejiste...

   // Success: proceed with opening or managing the PDF file
   MsgInfo( "PDF generated successfully!" )

   cCommand := "START " +cPdfFile

   // RUN ( cCommand )
   MYRUN ( cCommand )  // ADOBE ACROBAT

   ELSE

    // Error handling: might need to check command line exit code for detailed errors
    MsgStop( "Failed to generate PDF. Check if wkhtmltopdf is installed and accessible." )

   ENDIF

RETURN NIL

FUNCTION MYRUN( cComando )

   Local oShell, RET, lOk:=.T.

   TRY
      #IFDEF __XHARBOUR__
         oShell := CreateObject( "WScript.Shell" )
      #ELSE
         oShell := Win_OleCreateObject( "WScript.Shell" )
      #ENDIF

   CATCH

  TRY
     #IFDEF __XHARBOUR__
        oShell := CreateObject( "WScript.Shell" )
     #ELSE
        oShell := Win_OleCreateObject( "WScript.Shell" )
     #ENDIF

  CATCH

     lOk:=.F.

    END
   END

   IF lOk

  TRY

      //.F. = Nao espera a aplicacao fechar, .T. espera.
      oShell:Run( "%comspec% /c " + cComando, 0, .T. ) 

  CATCH

      lOk:=.F.

  END

  oShell := NIL

   ENDIF

   //--   RET := oShell:Run( "%comspec% /c " + cComando, 0)  //, .T. )

RETURN( lOk )

// FIN / END                     kapiabafwh@gmail.com

Regards, saludos.

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 883
Joined: Tue Oct 11, 2005 11:57 AM
Re: CONVERTIR html A pdf
Posted: Thu Dec 18, 2025 07:32 PM

Muchas Gracias....

Lo Reviso y les comento.

;-) Ji,ji,ji... buena la cosa... "all you need is code"

http://www.xdata.cl - Desarrollo Inteligente
----------
Asus TUF F15, 32GB Ram, 2 * 1 TB NVME M.2, GTX 1650

Continue the discussion