# Batch compress PDFs from the command line

> Compress every PDF in a folder on Windows, Linux or macOS with the pdfnetstack tool, losslessly, with a CSV report per file. MIT licensed.

Available since 1.0.

Optimize a whole folder of PDFs in one command, with a report of what each file gained.

## Short answer

Install the `pdfnetstack` tool and run `batch` with an input and an output folder. Each file is
optimized losslessly and verified, and `--report` writes one CSV row per document. Without an
output folder, `batch` only measures and writes nothing.

```bash
dotnet tool install -g PdfNetStack.Tool

pdfnetstack batch ./incoming ./optimized --report report.csv
pdfnetstack batch ./incoming                     # measure only, writes nothing
pdfnetstack batch ./incoming ./optimized --jpeg-quality 60 --jobs 4
```

`--jobs` sets how many documents run in parallel, `--threads` the threads per document. The tool
needs the .NET 8 or later runtime.

## The same in C#

```csharp title="recipe-batch-compress-pdf-cli.cs"
#:package PdfNetStack@1.2.0
using PdfNetStack;

Directory.CreateDirectory("optimized");
foreach (string path in Directory.GetFiles(".", "*.pdf"))
{
    string output = Path.Combine("optimized", Path.GetFileName(path));
    PdfOptimizationResult result = await PdfOptimizer.OptimizeFileAsync(path, output);
    Console.WriteLine($"{Path.GetFileName(path)}: {result.Disposition}, {result.SavingPercent:F1}% saved");
}
```

Files that are already well compressed come back as `OriginalRetained`: the rewrite saved less
than `MinSavingPercent` (1% by default), so the original bytes were kept.

## Related

- [Command line](https://www.jadev-corp.com/cli/): every command and option
- [Compress PDF](https://www.jadev-corp.com/guides/compress-pdf/)
- API: [PdfOptimizer](https://www.jadev-corp.com/api/pdfnetstack/pdfoptimizer/)

Source: https://www.jadev-corp.com/pdfnetstack/recipes/batch-compress-pdf-cli/
