Creare un file PDF con le funzioni esterne del API di Haru

Da Gambas-it.org - Wikipedia.

La libreria Haru è una libreria gratuita e multipiattaforma per generare file PDF.

In particolare essa consente di:

  • generare file PDF con linee, testo ed immagini;
  • dettagli, annotazioni testuali e annotazioni-collegamento;
  • inglobare immagini PNG e Jpeg;
  • inglobare font Type1 e TrueType.
  • creare file PDF criptati.


Per poter fruire delle risorse della libreria Haru in Gambas, è necessario richiamare la sua libreria dinamica condivisa attualmente: "libhpdf-2.2.1.so"


Mostriamo di seguito un semplice codice per generare un documento PDF formato da più pagine con testo colorato:

Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org>
 
Library "libhpdf-2.2.1"

Private Enum HPDF_PAGE_SIZE_LETTER = 0,
   HPDF_PAGE_SIZE_LEGAL,
   HPDF_PAGE_SIZE_A3,
   HPDF_PAGE_SIZE_A4,
   HPDF_PAGE_SIZE_A5,
   HPDF_PAGE_SIZE_B4,
   HPDF_PAGE_SIZE_B5,
   HPDF_PAGE_SIZE_EXECUTIVE,
   HPDF_PAGE_SIZE_US4x6,
   HPDF_PAGE_SIZE_US4x8,
   HPDF_PAGE_SIZE_US5x7,
   HPDF_PAGE_SIZE_COMM10,
   HPDF_PAGE_SIZE_EOF
   
Private Enum HPDF_PAGE_PORTRAIT = 0, HPDF_PAGE_LANDSCAPE

' HPDF_Doc HPDF_New (HPDF_Error_Handler user_error_fn, void *user_data)
' Create an instance of a document object and initialize it.
Private Extern HPDF_New(user_error_fn As Pointer, data As Pointer) As Pointer

' HPDF_Page HPDF_AddPage (HPDF_Doc pdf)
' Creates a new page and adds it after the last page of a document.
Private Extern HPDF_AddPage(HPDF_Doc As Pointer) As Pointer

' HPDF_STATUS HPDF_Page_SetSize (HPDF_Page page, HPDF_PageSizes size, HPDF_PageDirection direction)
' Changes the size and direction of a page to a predefined size.
Private Extern HPDF_Page_SetSize(HPDF_Page As Pointer, HPDF_PageSizes As Integer, HPDF_PageDirection As Integer) As Integer

' HPDF_REAL HPDF_Page_GetHeight (HPDF_Page page)
' Gets the height of a page.
Private Extern HPDF_Page_GetHeight(HPDF_Page As Pointer) As Single

' HPDF_Font HPDF_GetFont (HPDF_Doc pdf, const char *font_name, const char *encoding_name)
' Gets the handle of a requested font object.
Private Extern HPDF_GetFont(HPDF_Page As Pointer, font_name As String, encoding_name As String) As Pointer

' HPDF_STATUS HPDF_Page_BeginText (HPDF_Page page)
' Begins a text object and sets the text position to (0, 0).
Private Extern HPDF_Page_BeginText(HPDF_Page As Pointer) As Integer

' HPDF_STATUS HPDF_Page_SetFontAndSize (HPDF_Page page, HPDF_Font font, HPDF_REAL size)
' Sets the type of font and size leading.
Private Extern HPDF_Page_SetFontAndSize(HPDF_Page As Pointer, HPDF_Font As Pointer, size As Single) As Integer

' HPDF_STATUS HPDF_Page_TextOut (HPDF_Page page, HPDF_REAL xpos, HPDF_REAL ypos, const char *text)
' Prints the text on the specified position.
Private Extern HPDF_Page_TextOut(HPDF_Page As Pointer, xpos As Single, ypos As Single, text As String) As Integer

' HPDF_STATUS HPDF_Page_SetRGBFill (HPDF_Page  page, HPDF_REAL r, HPDF_REAL g, HPDF_REAL b)
' Sets the filling color.
Private Extern HPDF_Page_SetRGBFill(HPDF_Page As Pointer, HRr As Single, HRg As Single, HRb As Single) As Integer

' HPDF_STATUS HPDF_Page_EndText (HPDF_Page page)
' Ends a text object.
Private Extern HPDF_Page_EndText(HPDF_Page As Pointer) As Integer

' HPDF_STATUS HPDF_SaveToFile (HPDF_Doc pdf, const char *file_name)
' Saves the current document to a file.
Private Extern HPDF_SaveToFile(HPDF_Doc As Pointer, filename As String) As Integer

' void HPDF_Free (HPDF_Doc pdf)
' Revokes a document object and all resources.
Private Extern HPDF_Free(HPDF_Doc As Pointer)


Public Sub Main()

 Dim pdf, page, font As Pointer
 Dim altezza As Single
 Dim numero_pagina As Byte
 
  pdf = HPDF_New(0, 0)
  If IsNull(pdf) Then Error.Raise("Impossibile inizializzare la libreria 'hpdf' !")
   
  font = HPDF_GetFont(pdf, "Helvetica", Null)
  If IsNull(font) Then Error.Raise("Impossibile impostare il font !")
   
' Creiamo un documento PDF formato da 3 pagine:
  For numero_pagina = 1 To 3
' Aggiunge un nuovo oggetto pagina:
    page = HPDF_AddPage(pdf)
    If IsNull(page) Then Error.Raise("Impossibile creare una nuova pagina !")
   
    HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT)

    altezza = HPDF_Page_GetHeight(page)

    HPDF_Page_BeginText(page)
   
    HPDF_Page_SetFontAndSize(page, font, 12)

    HPDF_Page_SetRGBFill(page, 1.0, 0.0, 0.0)
    
    HPDF_Page_TextOut(page, 100, altezza - 100, "Testo di colore rosso della pagina num. " & numero_pagina)

    HPDF_Page_SetRGBFill(page, 0.0, 1.0, 0.0)
    
    HPDF_Page_TextOut(page, 100, altezza - 200, "testo di colore verde della pagina num. " & numero_pagina)
 
    HPDF_Page_SetRGBFill(page, 0.0, 0.0, 1.0)

    HPDF_Page_TextOut(page, 100, altezza - 300, "Testo di colore blu della pagina num. " & numero_pagina)
 
    HPDF_Page_EndText(page)
   
  Next
   
' Salva il documento creato in un file .pdf:
   HPDF_SaveToFile(pdf, "/percorso/del/file.pdf")
   

' Va in chiusura:
   HPDF_Free(pdf)

End



Riferimenti