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, realizzata da Takeshi Kanno e Antony Dovgal.

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 installare e richiamare la sua libreria dinamica condivisa attualmente: "libhpdf-2.3.0.so"


Mostriamo di seguito due esempi.


Primo esempio: viene generato un documento PDF formato da più pagine con testo colorato:

Library "libhpdf-2.3.0"

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


Secondo esempio: viene generato un documento PDF ad una pagina con testo colorato, con allineamento sia centrato che giustificato. Il testo viene impostato all'interno di un rettangolo che verrà colorato anch'esso per farlo vedere didatticamente; può essere nascosto assegnandogli i valori per il colore bianco. Viene inoltre caricata nel documento anche un'immagine:

Library "libhpdf-2.3.0"

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

Private Enum HPDF_TALIGN_LEFT = 0, HPDF_TALIGN_RIGHT, HPDF_TALIGN_CENTER, HPDF_TALIGN_JUSTIFY

' 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_GetWidth (HPDF_Page page)
' Gets the width of the page.
Private Extern HPDF_Page_GetWidth(HPDF_Page As Pointer) As Single

' 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_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_SetLineWidth (HPDF_Page page, HPDF_REAL line_width)
' Sets the width of the line used to stroke a path.
Private Extern HPDF_Page_SetLineWidth(HPDF_Page As Pointer, HRline_width As Single) As Integer
 
' HPDF_STATUS HPDF_Page_Rectangle (HPDF_Page page, HPDF_REAL  x, HPDF_REAL y, HPDF_REAL width, HPDF_REAL height)
' Appends a rectangle to the current path.
Private Extern HPDF_Page_Rectangle(HPDF_Page As Pointer, HRx As Single, HRy As Single, HRw As Single, HRh As Single) As Integer

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

' HPDF_STATUS HPDF_Page_Stroke (HPDF_Page page)
' Paints the current path.
Private Extern HPDF_Page_Stroke(HPDF_Page As Pointer) As Integer

' HPDF_STATUS HPDF_Page_TextRect (HPDF_Page page, HPDF_REAL left, HPDF_REAL top, HPDF_REAL right, HPDF_REAL bottom, const char *text, HPDF_TextAlignment align, HPDF_UINT *len)
' Prints the text inside the specified region.
Private Extern HPDF_Page_TextRect(HPDF_Page As Pointer, HRleft As Single, HRtop As Single, HRright As Single, HRbottom As Single, text As String, align As Integer, HUlen As Pointer) As Integer

' HPDF_Image HPDF_LoadPngImageFromFile (HPDF_Doc pdf, const char *filename)
' Loads an external PNG image file.
Private Extern HPDF_LoadPngImageFromFile(HPDF_Doc As Pointer, filename As String) As Pointer

' HPDF_UINT HPDF_Image_GetWidth (HPDF_Image image)
' Gets the width of the image of an image object.
Private Extern HPDF_Image_GetWidth(HPDF_Image As Pointer) As Integer

' HPDF_UINT HPDF_Image_GetHeight (HPDF_Image image)
' Gets the height of the image of an image object.
Private Extern HPDF_Image_GetHeight(HPDF_Image As Pointer) As Integer
 
' HPDF_STATUS HPDF_Page_DrawImage (HPDF_Page page, HPDF_Image image, HPDF_REAL  x, HPDF_REAL y, HPDF_REAL width, HPDF_REAL height)
' Shows an image in one operation.
Private Extern HPDF_Page_DrawImage(HPDF_Page As Pointer, HPDF_Image As Pointer, HRx As Single, HRy As Single, HRw As Single, HRh 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, immago As Pointer
 Dim larghezza, altezza As Single
 Dim iw, ih As Integer
 Dim testo As String = "Haru is a free, cross platform, open-sourced software library for generating PDF. " &
                       "You can add the feature of PDF creation by using Haru without understanding complicated internal structure of PDF. " &
                       "When you use it as shared-library, it can be used by many development languages which support shared library."

  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 !")
   
' 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)

  larghezza = HPDF_Page_GetWidth(page)

  altezza = HPDF_Page_GetHeight(page)
  

' Scrive il testo centrato:

' Colora il rettangolo contenente il testo:
  HPDF_Page_SetRGBStroke(page, 1.0, 0.0, 1.0)

' Colora il testo:
  HPDF_Page_SetRGBFill(page, 0.0, 0.0, 1.0)

  HPDF_Page_Rectangle(page, 25, altezza - 50, larghezza - 50, 20)
  HPDF_Page_Stroke(page)
  HPDF_Page_BeginText(page)
  HPDF_Page_SetFontAndSize(page, font, 13)
  HPDF_Page_TextRect(page, 0, altezza - 30, larghezza, 40, "Testo centrato", HPDF_TALIGN_CENTER, 0)
  HPDF_Page_EndText(page)
   
   
' Scrive il testo giustificato:

' Colora il rettangolo contenente il testo:
  HPDF_Page_SetRGBStroke(page, 1.0, 0.0, 1.0)
' Colora il testo:
  HPDF_Page_SetRGBFill(page, 0.0, 0.0, 1.0)

' Imposta la dimensione dei bordi del rettangolo:
  HPDF_Page_SetLineWidth(page, 5)

  HPDF_Page_Rectangle(page, 25, 50, larghezza - 50, altezza - 100)
  HPDF_Page_Stroke(page)
  HPDF_Page_BeginText(page)
  HPDF_Page_SetFontAndSize(page, font, 13)
  HPDF_Page_TextRect(page, 27, altezza - 70, larghezza - 27, 40, testo, HPDF_TALIGN_JUSTIFY, 0)
  HPDF_Page_EndText(page)
   
   
' Carica il file immagine:
  immago = HPDF_LoadPngImageFromFile(pdf, "/percorso/del/file.png")
   
  iw = HPDF_Image_GetWidth(immago)
  ih = HPDF_Image_GetHeight(immago)

' Imposta l'altezza della posizione dell'immagine nel documento:
  altezza -= 250

' Disegna l'immagine del documento:
  HPDF_Page_DrawImage(page, immago, 200, altezza, iw, ih)
  
' Salva il documento creato in un file .pdf:
  HPDF_SaveToFile(pdf, "/percorso/del/file.pdf")
  

' Va in chiusura:
  HPDF_Free(pdf)
 
End



Riferimenti