Comprimere un file PDF con le funzioni esterne del API di Ghostscript
Da Gambas-it.org - Wikipedia.
Per ridurre la quantità di byte di un file PDF, è possibile usare le risorse esterne del API di Ghostscript.
E' necessario avere installata nel sistema e usare in Gambas la libreria esterna condivisa: "libgs.so.10.04 ".
Va sottolineato che, facendo riferimento a un terminale, si userebbero i seguenti comandi:
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf
laddove per il comando "-dPDFSETTINGS" avremo:
- -dPDFSETTINGS=/screen qualità bassa, minore quantità di byte del PDF (72 dpi)
- -dPDFSETTINGS=/ebook per pdf di migliore qualità, ma leggermente più grandi (150 dpi)
- -dPDFSETTINGS=/prepress risultato simile all'impostazione "Prepress Optimized" di Acrobat Distiller (300 dpi)
- -dPDFSETTINGS=/printer risultato simile all'impostazione "Stampa ottimizzata" di Acrobat Distiller (300 dpi)
- -dPDFSETTINGS=/default seleziona l'output destinato a essere utile per un'ampia varietà di usi, eventualmente a spese di un file di output più grande.
Mostriamo un esempio pratico:
Private Const PDFORIGINARIO As String = "/percorso/del/file/pdf/originario" Private Const PDFFINALE As String = "/percorso/del/file/pdf/finale/compresso" Private pdfsettings As String[] = ["screen", "ebook", "prepress", "printer", "default"] Library "libgs:10.04" Private Enum GS_ARG_ENCODING_LOCAL = 0, GS_ARG_ENCODING_UTF8, GS_ARG_ENCODING_UTF16LE Private Const gs_error_ok As Integer = 0 ' int gsapi_new_instance(void * * pinstance, void * caller_handle) ' Create a new instance of Ghostscript. Private Extern gsapi_new_instance(pinstance As Pointer, caller_handle As Pointer) As Integer ' int gsapi_set_arg_encoding(void *instance, int encoding) ' Set the encoding used for the args. Private Extern gsapi_set_arg_encoding(instance As Pointer, encoding As Integer) As Integer ' int gsapi_init_with_args(void *instance, int argc, char **argv) ' Initialise the interpreter. Private Extern gsapi_init_with_args(instance As Pointer, argc As Integer, argv As Pointer) As Integer ' int gsapi_exit(void *instance) Private Extern gsapi_exit(instance As Pointer) As Integer ' void gsapi_delete_instance(void *instance) ' Destroy an instance of Ghostscript. Private Extern gsapi_delete_instance(instance As Pointer) Public Sub Main() Dim minst As Pointer Dim i As Integer Dim gsargv As String[] = [" ", "-sDEVICE=pdfwrite", "-dCompatibilityLevel=1.4", "-dPDFSETTINGS=" &/ pdfsettings[0], "-dNOPAUSE", "-dQUIET", "-dBATCH", "-sOutputFile=" &/ PDFFINALE, PDFORIGINARIO] i = gsapi_new_instance(VarPtr(minst), 0) If i < gs_error_ok Then Error.Raise("Error !") i = gsapi_set_arg_encoding(minst, GS_ARG_ENCODING_UTF8) If i == gs_error_ok Then code = gsapi_init_with_args(minst, gsargv.Count, gsargv.Data) i = gsapi_exit(minst) If i < gs_error_ok Then Error.Raise("Error !") gsapi_delete_instance(minst) End
Riferimenti
- https://ghostscript.readthedocs.io/en/latest/index.html
- https://ghostscript.com/docs/9.54.0/Use.htm
- https://gist.github.com/ahmed-musallam/27de7d7c5ac68ecbd1ed65b6b48416f9
- https://askubuntu.com/questions/113544/how-can-i-reduce-the-file-size-of-a-scanned-pdf-file
- https://askubuntu.com/questions/207447/how-to-reduce-the-size-of-a-pdf-file
- https://www.adobe.com/acrobat/hub/how-to-compress-pdf-in-linux.html
- https://www.tutorialspoint.com/unix_commands/gs.htm
- https://linux.die.net/man/1/gs
- https://www.geeksforgeeks.org/gs-command-in-linux-with-examples/