Link Search Menu Expand Document

PDF Optimize

Optimize PDF document to reduce its size.

Available methods

[POST] /pdf/optimize

  • url required. URL to the source file. Supports links from Google Drive, Dropbox and from built-in PDF.co files storage. For uploading files via API please check Files Upload section. If you are randomly getting Too Many Requests or Access Denied error for your input url, please try to add cache: to enable built-in url caching.
  • name optional. File name for generated output. Must be a String.
  • password optional. Password of PDF file. Must be a String.
  • async optional. Runs processing asynchronously. Returns Use JobId that you may use with /job/check to check state of the processing (possible states: working, failed, aborted and success). Must be one of: true, false.
  • encrypt optional.Enable encryption for output file. Must be one of: true, false.
  • profiles optional. Must be a String. You can set additional and extra options using this parameter that allows you to set custom configuration. See profiles samples for examples.

Description

  • Method: POST
  • URL: /v1/pdf/optimize

Query parameters

No query parameters accepted.

Body payload

keyvaluedescriptiondisabled 
x-api-key Optional. Please use http headers instead when possible because passing your API key as parameter for POST or GET is not secure and is for legacy app compatibility only.  
true    
async optional. Runs processing asynchronously. Returns Use JobId that you may use with /job/check to check state of the processing (possible states: working, failed, aborted and success). Must be one of: true, false.texttrue
encrypt optional.Enable encryption for output file. Must be one of: true, false.texttrue
urlhttps://bytescout-com.s3-us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-optimize/sample.pdfrequired. URL of the source file. Must be a String. To use your file please upload it first to the temporary storage, see Upload and Manage Files section below to learn how to do it.text 
nameresult.pdfoptional. File name for generated output. Must be a String.text 
password optional. Password of PDF file. Must be a String.text 

Example responses

/pdf/optimize
{
    "url": "https://pdf-temp-files.s3.amazonaws.com/c3bde960dcc54bcebc14a72ab1521fa9/result-compressed.pdf",
    "fileSize": 798621,
    "pageCount": 5,
    "error": false,
    "status": 200,
    "name": "result-compressed.pdf",
    "remainingCredits": 59877
}

Code Snippets

CURL
curl --location --request POST 'https://api.pdf.co/v1/pdf/optimize' \
--header 'Content-Type: application/json' \
--header 'x-api-key: ' \
--form 'url="https://bytescout-com.s3-us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-optimize/sample.pdf"' \
--form 'name="result.pdf"' \
--form 'password=""'
JavaScript
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("x-api-key", "");

var formdata = new FormData();
formdata.append("url", "https://bytescout-com.s3-us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-optimize/sample.pdf");
formdata.append("name", "result.pdf");
formdata.append("password", "");

var requestOptions = {
	method: 'POST',
	headers: myHeaders,
	body: formdata,
	redirect: 'follow'
};

fetch("https://api.pdf.co/v1/pdf/optimize", requestOptions)
	.then(response => response.text())
	.then(result => console.log(result))
	.catch(error => console.log('error', error));
NodeJs
var request = require('request');
var options = {
	'method': 'POST',
	'url': 'https://api.pdf.co/v1/pdf/optimize',
	'headers': {
		'Content-Type': 'application/json',
		'x-api-key': ''
	},
	formData: {
		'url': 'https://bytescout-com.s3-us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-optimize/sample.pdf',
		'name': 'result.pdf',
		'password': ''
	}
};
request(options, function (error, response) {
	if (error) throw new Error(error);
	console.log(response.body);
});

PHP
<?php

$curl = curl_init();

curl_setopt_array($curl, array(
	CURLOPT_URL => 'https://api.pdf.co/v1/pdf/optimize',
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_ENCODING => '',
	CURLOPT_MAXREDIRS => 10,
	CURLOPT_TIMEOUT => 0,
	CURLOPT_FOLLOWLOCATION => true,
	CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	CURLOPT_CUSTOMREQUEST => 'POST',
	CURLOPT_POSTFIELDS => array('url' => 'https://bytescout-com.s3-us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-optimize/sample.pdf','name' => 'result.pdf','password' => ''),
	CURLOPT_HTTPHEADER => array(
		'Content-Type: application/json',
		'x-api-key: '
	),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

Java
import java.io.*;
import okhttp3.*;
public class main {
	public static void main(String []args) throws IOException{
		OkHttpClient client = new OkHttpClient().newBuilder()
			.build();
		MediaType mediaType = MediaType.parse("application/json");
		RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
			.addFormDataPart("url","https://bytescout-com.s3-us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-optimize/sample.pdf")
			.addFormDataPart("name","result.pdf")
			.addFormDataPart("password","")
			.build();
		Request request = new Request.Builder()
			.url("https://api.pdf.co/v1/pdf/optimize")
			.method("POST", body)
			.addHeader("Content-Type", "application/json")
			.addHeader("x-api-key", "")
			.build();
		Response response = client.newCall(request).execute();
		System.out.println(response.body().string());
	}
}

C#
using System;
using RestSharp;
namespace HelloWorldApplication {
	class HelloWorld {
		static void Main(string[] args) {
			var client = new RestClient("https://api.pdf.co/v1/pdf/optimize");
			client.Timeout = -1;
			var request = new RestRequest(Method.POST);
			request.AddHeader("Content-Type", "application/json");
			request.AddHeader("x-api-key", "");
			request.AlwaysMultipartFormData = true;
			request.AddParameter("url", "https://bytescout-com.s3-us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-optimize/sample.pdf");
			request.AddParameter("name", "result.pdf");
			request.AddParameter("password", "");
			IRestResponse response = client.Execute(request);
			Console.WriteLine(response.Content);
		}
	}
}

Python
import requests
import json

url = "https://api.pdf.co/v1/pdf/optimize"

payload={'url': 'https://bytescout-com.s3-us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-optimize/sample.pdf',
'name': 'result.pdf',
'password': ''}
files=[

]
headers = {
	'Content-Type': 'application/json',
	'x-api-key': ''
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)

Powershell
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("x-api-key", "")

$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()
$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$stringHeader.Name = "url"
$stringContent = [System.Net.Http.StringContent]::new("https://bytescout-com.s3-us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-optimize/sample.pdf")
$stringContent.Headers.ContentDisposition = $stringHeader
$multipartContent.Add($stringContent)

$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$stringHeader.Name = "name"
$stringContent = [System.Net.Http.StringContent]::new("result.pdf")
$stringContent.Headers.ContentDisposition = $stringHeader
$multipartContent.Add($stringContent)

$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$stringHeader.Name = "password"
$stringContent = [System.Net.Http.StringContent]::new("")
$stringContent.Headers.ContentDisposition = $stringHeader
$multipartContent.Add($stringContent)

$body = $multipartContent

$response = Invoke-RestMethod 'https://api.pdf.co/v1/pdf/optimize' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json