Welcome Guest Search | Active Topics | Sign In | Register

Options for reducing the size of a PDF Options
Phil
Posted: Monday, December 8, 2025 8:56:16 AM
Rank: Advanced Member
Groups: Member

Joined: 11/8/2017
Posts: 80
Hello - I wondered if you any thoughts about reducing the file size of a PDF (generated by EO-PDF) that has several (repeated [SVG]) images. At the moment several (chunked) PDF are generated as follows
Code: C#
//...
HtmlToPdfOptions htmlToPdfOptions = new HtmlToPdfOptions();
//...
HtmlToPdfResult htmlToPdfResult = null;
using (var debugConsoleLog = new StringWriter())
{
    HtmlToPdf.DebugConsole = debugConsoleLog;
    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    {
        _logger.LogInformation($"Generate PDF-generateFromCoversheetHtml: {generateFromCoversheetHtml}");
        try
        {
            htmlToPdfResult = HtmlToPdf.ConvertHtml(inputHtml, stream, htmlToPdfOptions);
        }
        catch (Exception ex)
        {
            //...
        }
        bytes = stream.ToArray();
    }
    debugConsoleLog.Flush();
}
//...

...then these 'chunks' need to be merged - we would like to do this via EO-PDF (see the code below) however as this is slow by comparison we merge via a 3rd party tool

Code: C#
var pdfDocuments = new List<EO.Pdf.PdfDocument>();

List<byte[]> listOfBytes = GetListOfPdfs(...pdf-chunks...);

EO.Pdf.PdfDocument pdfDocument = null;
foreach (var pdfAsArray in listOfBytes)
{
    if (pdfAsArray.Length > 0)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            stream.Write(pdfAsArray, 0, pdfAsArray.Length);
            stream.Seek(0, SeekOrigin.Begin);
            pdfDocument = new EO.Pdf.PdfDocument(stream);
            pdfDocuments.Add(pdfDocument);
        }
    }
}
pdfDocument = EO.Pdf.PdfDocument.Merge(pdfDocuments.ToArray());
using (MemoryStream stream = new MemoryStream())
{
    pdfDocument.Save(stream);
    merged = stream.ToArray();
    pdfDocument = null;
}

So the options I have
- perhaps reduce the size of the PDF chunks being generated (is there a way to do this - eg. different HtmlToPdf.ConvertHtml overload, 'declarifying' (eg. reduce dpi) w/o losing too much clarity)
- I know that the EO-PDF merge consolidates fonts, etc and hence the file size is likely smaller, but is there a way to make this (the EO-PDF merge) more performant

Kind regards
Phil
eo_support
Posted: Monday, December 8, 2025 9:37:00 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,440
Hi,

Unfortunately no. The best way to reduce file size is to try to avoid SVG rendering from rasterizing the rendering. The Chromium browser engine will always try to rasterize the vector rendering for certain SVG features. For example, a SVG pattern will always result in that pattern being rasterized. In that case instead of repeating a pattern inside a SVG file, you can save the pattern itself in a separate SVG file and then use HTML to repeat that SVG file (for example, using multiple img tags). That will significantly reduces the PDF file size thus improve merging as well.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.