Compress PDFs
Reduce the size of PDF files by compressing to smaller sizes for lower bandwidth viewing, downloading, and sharing.
Support for multiple compression levels to retain the quality of images and graphics
Rest API
See our public API Reference for Compress PDF
Compress PDFs
Compress PDFs to reduce the file size prior to performing workflow operations that use bandwidth or memory.
Please refer the API usage guide to understand how to use our APIs.
Java
.NET
Node JS
Rest API
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples2// Run the sample:3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.compresspdf.CompressPDF45 public class CompressPDF {6 // Initialize the logger.7 private static final Logger LOGGER = LoggerFactory.getLogger(CompressPDF.class);89 public static void main(String[] args) {1011 try {12 // Initial setup, create credentials instance.13 Credentials credentials = Credentials.servicePrincipalCredentialsBuilder()14 .withClientId("PDF_SERVICES_CLIENT_ID")15 .withClientSecret("PDF_SERVICES_CLIENT_SECRET")16 .build();1718 // Create an ExecutionContext using credentials and create a new operation instance.19 ExecutionContext executionContext = ExecutionContext.create(credentials);20 CompressPDFOperation compressPDFOperation = CompressPDFOperation.createNew();2122 // Set operation input from a source file.23 FileRef source = FileRef.createFromLocalFile("src/main/resources/compressPDFInput.pdf");24 compressPDFOperation.setInput(source);2526 // Execute the operation27 FileRef result = compressPDFOperation.execute(executionContext);2829 // Save the result at the specified location30 result.saveAs("output/compressPDFOutput.pdf");3132 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {33 LOGGER.error("Exception encountered while executing operation", ex);34 }35 }36 }
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_net_samples2// Run the sample:3// cd CompressPDF/4// dotnet run CompressPDF.csproj56 namespace CompressPDF7 {8 class Program9 {10 private static readonly ILog log = LogManager.GetLogger(typeof(Program));11 static void Main()12 {13 //Configure the logging14 ConfigureLogging();15 try16 {17 // Initial setup, create credentials instance.18 Credentials credentials = Credentials.ServicePrincipalCredentialsBuilder()19 .WithClientId("PDF_SERVICES_CLIENT_ID")20 .WithClientSecret("PDF_SERVICES_CLIENT_SECRET")21 .Build();2223 // Create an ExecutionContext using credentials and create a new operation instance.24 ExecutionContext executionContext = ExecutionContext.Create(credentials);25 CompressPDFOperation compressPDFOperation = CompressPDFOperation.CreateNew();2627 // Set operation input from a source file.28 FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"compressPDFInput.pdf");29 compressPDFOperation.SetInput(sourceFileRef);3031 // Execute the operation.32 FileRef result = compressPDFOperation.Execute(executionContext);3334 // Save the result to the specified location.35 result.SaveAs(Directory.GetCurrentDirectory() + "/output/compressPDFOutput.pdf");36 }37 catch (ServiceUsageException ex)38 {39 log.Error("Exception encountered while executing operation", ex);40 }41 // Catch more errors here . . .42 }4344 static void ConfigureLogging()45 {46 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());47 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));48 }49 }50 }
Copied to your clipboard1// Get the samples from http://www.adobe.com/go/pdftoolsapi_node_sample2// Run the sample:3// node src/compresspdf/compress-pdf.js45 const PDFServicesSdk = require('@adobe/pdfservices-node-sdk');67 try {8 // Initial setup, create credentials instance.9 const credentials = PDFServicesSdk.Credentials10 .servicePrincipalCredentialsBuilder()11 .withClientId("PDF_SERVICES_CLIENT_ID")12 .withClientSecret("PDF_SERVICES_CLIENT_SECRET")13 .build();1415 // Create an ExecutionContext using credentials and create a new operation instance.16 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials),17 compressPDF = PDFServicesSdk.CompressPDF,18 compressPDFOperation = compressPDF.Operation.createNew();1920 // Set operation input from a source file.21 const input = PDFServicesSdk.FileRef.createFromLocalFile('resources/compressPDFInput.pdf');22 compressPDFOperation.setInput(input);2324 // Execute the operation and Save the result to the specified location.25 compressPDFOperation.execute(executionContext)26 .then(result => result.saveAsFile('output/compressPDFOutput.pdf'))27 .catch(err => {28 if(err instanceof PDFServicesSdk.Error.ServiceApiError29 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {30 console.log('Exception encountered while executing operation', err);31 } else {32 console.log('Exception encountered while executing operation', err);33 }34 });35 } catch (err) {36 console.log('Exception encountered while executing operation', err);37 }
Copied to your clipboard1// Please refer our Rest API docs for more information2// https://developer.adobe.com/document-services/docs/apis/#tag/Compress-PDF34curl --location --request POST 'https://pdf-services.adobe.io/operation/compresspdf' \5--header 'x-api-key: {{Placeholder for client_id}}' \6--header 'Content-Type: application/json' \7--header 'Authorization: Bearer {{Placeholder for token}}' \8--data-raw '{9 "assetID": "urn:aaid:AS:UE1:23c30ee0-2e4d-46d6-87f2-087832fca718"10}'1112// Legacy API can be found here13// https://documentcloud.adobe.com/document-services/index.html#post-compressPDF
Compress PDFs with Compression Level
Compress PDFs to reduce the file size on the basis of provided
compression level, prior to performing workflow operations that use
bandwidth or memory. Refer to CompressionLevel
in the API docs for a
list of supported compression levels.
Please refer the API usage guide to understand how to use our APIs.
Java
.NET
Node JS
Rest API
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples2// Run the sample:3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.compresspdf.CompressPDFWithOptions45 public class CompressPDFWithOptions {6 // Initialize the logger.7 private static final Logger LOGGER = LoggerFactory.getLogger(CompressPDFWithOptions.class);89 public static void main(String[] args) {1011 try {12 // Initial setup, create credentials instance.13 Credentials credentials = Credentials.servicePrincipalCredentialsBuilder()14 .withClientId("PDF_SERVICES_CLIENT_ID")15 .withClientSecret("PDF_SERVICES_CLIENT_SECRET")16 .build();1718 // Create an ExecutionContext using credentials and create a new operation instance.19 ExecutionContext executionContext = ExecutionContext.create(credentials);20 CompressPDFOperation compressPDFOperation = CompressPDFOperation.createNew();2122 // Set operation input from a source file.23 FileRef source = FileRef.createFromLocalFile("src/main/resources/compressPDFInput.pdf");24 compressPDFOperation.setInput(source);2526 // Build CompressPDF options from supported compression levels and set them into the operation27 CompressPDFOptions compressPDFOptions = CompressPDFOptions.compressPDFOptionsBuilder()28 .withCompressionLevel(CompressionLevel.LOW)29 .build();30 compressPDFOperation.setOptions(compressPDFOptions);3132 // Execute the operation33 FileRef result = compressPDFOperation.execute(executionContext);3435 // Save the result at the specified location36 result.saveAs("output/compressPDFWithOptionsOutput.pdf");3738 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {39 LOGGER.error("Exception encountered while executing operation", ex);40 }41 }42 }
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_net_samples2// Run the sample:3// cd CompressPDF/4// dotnet run CompressPDFWithOptions.csproj56 namespace CompressPDFWithOptions7 {8 class Program9 {10 private static readonly ILog log = LogManager.GetLogger(typeof(Program));11 static void Main()12 {13 //Configure the logging14 ConfigureLogging();15 try16 {17 // Initial setup, create credentials instance.18 Credentials credentials = Credentials.ServicePrincipalCredentialsBuilder()19 .WithClientId("PDF_SERVICES_CLIENT_ID")20 .WithClientSecret("PDF_SERVICES_CLIENT_SECRET")21 .Build();2223 // Create an ExecutionContext using credentials and create a new operation instance.24 ExecutionContext executionContext = ExecutionContext.Create(credentials);25 CompressPDFOperation compressPDFOperation = CompressPDFOperation.CreateNew();2627 // Set operation input from a source file.28 FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"compressPDFInput.pdf");29 compressPDFOperation.SetInput(sourceFileRef);3031 // Build CompressPDF options from supported compression levels and set them into the operation32 CompressPDFOptions compressPDFOptions = CompressPDFOptions.CompressPDFOptionsBuilder()33 .WithCompressionLevel(CompressionLevel.LOW)34 .Build();35 compressPDFOperation.SetOptions(compressPDFOptions);3637 // Execute the operation.38 FileRef result = compressPDFOperation.Execute(executionContext);3940 // Save the result to the specified location.41 result.SaveAs(Directory.GetCurrentDirectory() + "/output/compressPDFWithOptionsOutput.pdf");42 }43 catch (ServiceUsageException ex)44 {45 log.Error("Exception encountered while executing operation", ex);46 }47 // Catch more errors here . . .48 }4950 static void ConfigureLogging()51 {52 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());53 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));54 }55 }56 }
Copied to your clipboard1// Get the samples from http://www.adobe.com/go/pdftoolsapi_node_sample2// Run the sample:3// node src/compresspdf/compress-pdf-with-options.js45 const PDFServicesSdk = require('@adobe/pdfservices-node-sdk');67 try {8 // Initial setup, create credentials instance.9 const credentials = PDFServicesSdk.Credentials10 .servicePrincipalCredentialsBuilder()11 .withClientId("PDF_SERVICES_CLIENT_ID")12 .withClientSecret("PDF_SERVICES_CLIENT_SECRET")13 .build();1415 // Create an ExecutionContext using credentials and create a new operation instance.16 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials),17 compressPDF = PDFServicesSdk.CompressPDF,18 compressPDFOperation = compressPDF.Operation.createNew();1920 // Set operation input from a source file.21 const input = PDFServicesSdk.FileRef.createFromLocalFile('resources/compressPDFInput.pdf');22 compressPDFOperation.setInput(input);2324 // Provide any custom configuration options for the operation.25 const options = new compressPDF.options.CompressPDFOptions.Builder()26 .withCompressionLevel(PDFServicesSdk.CompressPDF.options.CompressionLevel.MEDIUM)27 .build();28 compressPDFOperation.setOptions(options);2930 // Execute the operation and Save the result to the specified location.31 compressPDFOperation.execute(executionContext)32 .then(result => result.saveAsFile('output/compressPDFWithOptionsOutput.pdf'))33 .catch(err => {34 if(err instanceof PDFServicesSdk.Error.ServiceApiError35 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {36 console.log('Exception encountered while executing operation', err);37 } else {38 console.log('Exception encountered while executing operation', err);39 }40 });41 } catch (err) {42 console.log('Exception encountered while executing operation', err);43 }
Copied to your clipboard1// Please refer our Rest API docs for more information2// https://developer.adobe.com/document-services/docs/apis/#tag/Compress-PDF34curl --location --request POST 'https://pdf-services.adobe.io/operation/compresspdf' \5--header 'x-api-key: {{Placeholder for client_id}}' \6--header 'Content-Type: application/json' \7--header 'Authorization: Bearer {{Placeholder for token}}' \8--data-raw '{9 "assetID": "urn:aaid:AS:UE1:23c30ee0-2e4d-46d6-87f2-087832fca718",10 "compressionLevel": "MEDIUM"11}'1213// Legacy API can be found here14// https://documentcloud.adobe.com/document-services/index.html#post-compressPDF