Compress PDF Image JBIG3 - PHP Sample Code

Sample code for using Apryse SDK to recompress bitonal (black and white) images in existing PDF documents using JBIG2 compression (lossless or lossy). The sample is intended to show how to specify hint information for the image encoder and is not meant to be a generic PDF optimization tool. To demonstrate the possible compression rates, we recompressed a document containing 17 scanned pages. The original input document is ~1.4MB and is using standard CCITT Fax compression. Lossless JBIG2 compression shrunk the filesize to 641KB, while lossy JBIG2 compression shrunk it to 176KB. Capabilities include programatically creating new fields and widget annotations, form filling, modifying existing field values, form templating, and flattening form fields.

Learn more about our Server SDK.

1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10// Relative path to the folder containing the test files.
11$input_path = getcwd()."/../../TestFiles/";
12$output_path = $input_path."Output/";
13
14// This sample project illustrates how to recompress bi-tonal images in an
15// existing PDF document using JBIG2 compression. The sample is not intended
16// to be a generic PDF optimization tool.
17//
18// You can download the entire document using the following link:
19// http://www.pdftron.com/net/samplecode/data/US061222892.pdf
20
21 PDFNet::Initialize($LicenseKey);
22 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
23
24 $pdf_doc = new PDFDoc("../../TestFiles/US061222892-a.pdf") ;
25 $pdf_doc->InitSecurityHandler();
26
27 $cos_doc = $pdf_doc->GetSDFDoc();
28 $num_objs = $cos_doc->XRefSize();
29 for($i = 1; $i < $num_objs; ++$i)
30 {
31 $obj = $cos_doc->GetObj($i);
32 if($obj && !$obj->IsFree() && $obj->IsStream())
33 {
34 // Process only images
35 $itr = $obj->Find("Subtype");
36 if(!$itr->HasNext() || $itr->Value()->GetName() != "Image")
37 continue;
38
39 $input_image = new Image($obj);
40 // Process only gray-scale images
41 if($input_image->GetComponentNum() != 1)
42 continue;
43 $bpc = $input_image->GetBitsPerComponent();
44 if($bpc != 1) // Recompress only 1 BPC images
45 continue;
46
47 // Skip images that are already compressed using JBIG2
48 $itr = $obj->Find("Filter");
49 if ($itr->HasNext() && $itr->Value()->IsName() &&
50 $itr->Value()->GetName() == "JBIG2Decode") continue;
51
52 $filter=$obj->GetDecodedStream();
53 $reader = new FilterReader($filter);
54
55
56 $hint_set = new ObjSet(); // A hint to image encoder to use JBIG2 compression
57 $hint=$hint_set->CreateArray();
58
59 $hint->PushBackName("JBIG2");
60 $hint->PushBackName("Lossless");
61
62 $new_image = Image::Create($cos_doc, $reader,
63 $input_image->GetImageWidth(),
64 $input_image->GetImageHeight(), 1, ColorSpace::CreateDeviceGray(), $hint);
65
66 $new_img_obj = $new_image->GetSDFObj();
67 $itr = $obj->Find("Decode");
68 if($itr->HasNext())
69 $new_img_obj->Put("Decode", $itr->Value());
70 $itr = $obj->Find("ImageMask");
71 if ($itr->HasNext())
72 $new_img_obj->Put("ImageMask", $itr->Value());
73 $itr = $obj->Find("Mask");
74 if ($itr->HasNext())
75 $new_img_obj->Put("Mask", $itr->Value());
76
77 $cos_doc->Swap($i, $new_img_obj->GetObjNum());
78 }
79 }
80
81 $pdf_doc->Save("../../TestFiles/Output/US061222892_JBIG2.pdf", SDFDoc::e_remove_unused);
82 $pdf_doc->Close();
83 PDFNet::Terminate();
84 echo "Done.";
85?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales