Some test text!

Discord Logo

Chat with us

PDFTron is now Apryse, learn more here.

Php / Guides / Optimize



PDFTron is now Apryse, learn more here.

Optimize & compress PDFs in PHP

To optimize a PDF with default settings.

$doc = new PDFDoc($filename);

Compress & optimize PDF files
Full code sample which shows how to use 'pdftron.PDF.Optimizer' to reduce PDF file size by removing redundant information and compressing data streams using the latest in image compression technology.

About optimize and compress

Compression as a subset of optimization represents encoding specific data using fewer bits than the original content by reducing the size of the data. This is distinct from optimize (which modifies all images) because you have the ability to choose individual images and to selectively choose the compression type for each.

Apryse SDK supports all basic and advanced compression filters allowed in PDF including:

  • JPEG2000
  • JBIG2
  • CCITT Fax
  • Flate/PNG
  • Crypt Filters

Compress images in a PDF document

To compress images using JBIG2 compression inside a PDF.

$doc = new PDFDoc($filename);
$cos_doc = $pdf_doc->GetSDFDoc();
$num_objs = $cos_doc->XRefSize();
for($i = 1; $i < $num_objs; ++$i) 
	$obj = $cos_doc->GetObj($i);
	if(!$obj || $obj->IsFree() || !$obj->IsStream())

	// Process only images
	$itr = $obj->Find("Subtype");
	if(!$itr->HasNext() || $itr->Value()->GetName() != "Image")
	$input_image = new Image($obj);

	// Process only gray-scale images
	if($input_image->GetComponentNum() != 1)

	$bpc = $input_image->GetBitsPerComponent();
	if($bpc != 1)	// Recompress only 1 BPC images

	// Skip images that are already compressed using JBIG2
	$itr = $obj->Find("Filter");
	if ($itr->HasNext() && $itr->Value()->IsName() && $itr->Value()->GetName() == "JBIG2Decode") continue; 

	$reader = new FilterReader($filter);

	$hint_set = new ObjSet(); 	// A hint to image encoder to use JBIG2 compression

	$new_image = Image::Create(
	$cos_doc->Swap($i, $new_img->GetSDFDoc()->GetObjNum());

PDF image JBIG2 compression
Full sample code which illustrates how to recompress bitonal (black and white) images in existing PDF documents using JBIG2 compression.

Get the answers you need: Support