[Gambas-user] How to reduce the size of PDFs generated with PDFSurface/Cairo

vuott at tutanota.com vuott at tutanota.com
Tue Oct 10 13:04:30 CEST 2023


Ehmmm...I got this simple and short code by using GhostScript external resources:


Private Const PDFPATH As String = "/path/of/original/pdf/file"      ' INPUT pdf file
Private Const OUTPUTPDFPATH As String = "/path/of/output/pdf/file"  ' OUTPUT pdf file
Private pdfsettings As String[] = ["screen", "ebook", "prepress", "printer", "default"]


Library "libgs:9.55"

Private Enum GS_ARG_ENCODING_LOCAL = 0, GS_ARG_ENCODING_UTF8, GS_ARG_ENCODING_UTF16LE
Private Enum gs_error_ok = 0, gs_error_Quit = -101

' 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 code As Integer
  Dim gsargv As String[] = [" ", "-sDEVICE=pdfwrite", "-dCompatibilityLevel=1.4", "-dPDFSETTINGS=" &/ pdfsettings[0], "-dNOPAUSE", "-dQUIET", "-dBATCH", "-sOutputFile=" &/ OUTPUTPDFPATH, INPUTPDFPATH]

  code = gsapi_new_instance(VarPtr(minst), 0)
  If code < gs_error_ok Then Error.Raise("Error !")

  code = gsapi_set_arg_encoding(minst, GS_ARG_ENCODING_UTF8)
  If code == gs_error_ok Then code = gsapi_init_with_args(minst, gsargv.Count, gsargv.Data)

  code = gsapi_exit(minst)
  If code < gs_error_ok Then Error.Raise("Error !")

  gsapi_delete_instance(minst)

End

-----------------------------------




9 ott 2023, 21:52 da claus.dietrich at freenet.de:

>> On 9th Oct Fabien Bodard gambas.fr at gmail.com wrote:
>> Pdf size is a big deal fore too.
>> I will have a look
>>
>>> Le lun. 9 oct. 2023 à 10:10, BB <adamnt42 at gmail.com  <http://lists.gambas-basic.org/listinfo/user>> a écrit :
>>>
>>> //>>/On 30/9/23 1:05 am, Claus Dietrich wrote: />>//>>/Hi />>//>>/I am using the Gambas PDFSurface and Cairo to generate and assemble />>/multi-page PDFs. />>//>>/When I insert a jpg-image from my scanner (A4 size, 300dpi, 90% quality) />>/with a size of 730 kB the resulting single page PDF is blown up to a size />>/of 2.8 MB. A stripped down demo project is attached: />>//>>/https://send.vis.ee/download/62247b9c0d0529a1/#JidKAJn64IdF4G1c-4B07A />>//>>/Does anyone know a way to obtain a PDF file with a similar size (resp. />>/compression) like the source image by using native Gambas means (=without />>/installing tools like ImageMagick)? />>//>>/Best regards />>/Claus />>//>>//>>/----[ http://gambaswiki.org/wiki/doc/netiquette ]---- />>//>>/Hi Claus, I don't see that you ever got a reply on this, so just a bit of />>/information from me on it. I don't know how you could reduce the size as />>/from my "memory" pdf used to and maybe still does include all kinds of />>/images as a dot-by-dot set of "portable" print instructions. That is for />>/each image pixel it creates approximately 4 pdf (byte code) statements. />>/Years ago I remember I had to tell the (ahem) "less experienced" graphics />>/designers never to use any shade of yellow in their pictures as that blew />>/the pdf size out by about 64 bytes per pixel i.e about 32 times the
>>>
> size of />>/the image. So 4 times the original image size would be a "good outcome" in />>/my opinion. As far as I know pdf still doesn't know fig-all about image />>/formats. />>//>>/hth />>//>>/b Hi The PDF surface seems to blow up the pixel graphics as required by its dimensions. Hence the format or the source (png or jpg) doesn't play a decisive role. So I decided to go for the best quality by always using png-graphics and after the PDF is generated with the PDFSurface I now compress it with a ghostscript shell command. I simply use the parameter -dPDFSETTINGS, i.e. ///-dPDFSETTINGS/=150 to reduce the PDF to 150 dpi. A more sophisticated solution with ghostscript is described here: https://gist.github.com/ahmed-musallam/27de7d7c5ac68ecbd1ed65b6b48416f9 I am not sure whether this is the best/right way, but it works and the results fulfil my requirements. Brgds Claus /
>
>
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20231010/079027b4/attachment.htm>


More information about the User mailing list