Optimize
Optimize will enable customized compression & optimization of PDF files. It lets you configure the compression type, set the resolution, choose whether you would like to clip the invisible parts of the images in your PDF and much more.
Code Samples
Try the API in the language you prefer
- C#
- Java
- JavaScript
- PHP
- Python
- Ruby
// create optimize object
Optimize optimize = new Optimize()
{
// document
Document = new Document()
{
DocData = File.ReadAllBytes("myPdf.pdf"),
Name = "myPdf.pdf",
},
// action
OptimizeAction = new OptimizeAction()
{
UseProfile = true,
Profile = OptimizeActionProfile.Max
},
};
// optimize
var res = await Pdf4meClient.Pdf4me.Instance.OptimizeClient.OptimizeAsync(optimize);
// extract the optimized PDF and writing it to disk
byte[] optimizedPdf = res.Document.DocData;
File.WriteAllBytes("optimizedPdf.pdf", optimizedPdf);
// setup the optimizeClient
OptimizeClient optimizeClient = new OptimizeClient(pdf4meClient);
// create optimize object
Optimize optimize = new Optimize();
// document
Document document = new Document();
document.setDocData(Files.readAllBytes(Paths.get("myPdf.pdf")));
optimize.setDocument(document);
// action
OptimizeAction optimizeAction = new OptimizeAction();
optimizeAction.useProfile(true);
optimizeAction.profile(ProfileEnum.MAX);
optimize.setOptimizeAction(optimizeAction);
// optimization
OptimizeRes res = optimizeClient.optimize(optimize);
// extracting the generated PDF and writing it to disk
byte[] optimizedPdf = res.getDocument().getDocData();
FileUtils.writeByteArrayToFile(new File("optimizedPdf.pdf"), optimizedPdf);
// setup the pdf4meClient
const pdf4meClient = pdf4me.createClient('YOUR API KEY')
// create optimize object
const optimizeReq = {
// document
document: {
docData: fs.readFileSync(path.join(__dirname, 'myPdf.pdf')).toString('base64'),
},
// action
optimizeAction: {
useProfile: true,
profile: 'web',
},
}
// optimize
pdf4meClient.optimize(optimizeReq)
.then(function(optimizeRes) {
// extracting the generated PDF and writing it to disk
const pdfDocument = Buffer.from(optimizeRes.document.docData, 'base64')
fs.writeFileSync(path.join(__dirname, 'optimizedPdf.pdf'), pdfDocument)
})
.catch(error => {
console.log(error)
process.exit(1)
})
# setup the optimize_client
optimize_client = OptimizeClient(pdf4me_client)
# create the optimize object
optimize = Optimize(
document=Document(
doc_data=FileReader().get_file_data('myPdf.pdf')
),
optimize_action=OptimizeAction(
use_profile=True,
profile='max'
)
)
# optimization
res = optimize_client.optimize(optimize=optimize)
# extracting the generated PDF
optimized_pdf = base64.b64decode(res['document']['doc_data'])
# writing it to disk
with open('optimizedPdf.pdf', 'wb') as f:
f.write(optimized_pdf)
pdf4me()->optimize([
'document' => [
'docData' => $client->getFileData('myPdf.pdf')
],
'optimizeAction' => [
'profile' => 'max',
'useProfile' => true
]
]);
// extracting the optimized PDF
$optimizedPdf = base64_decode($createOptimize->document->docData);
// and writing it to file
file_put_contents('optimizedPdf.pdf', $optimizedPdf);
file = '/myPdf.pdf'
a = Pdf4me::Optimize.new(
document: Pdf4me::Document.new(
doc_data: Base64.encode64(File.open(file, 'rb', &:read))
),
optimize_action: Pdf4me::OptimizeAction.new(
use_profile: true,
profile: 'max'
)
)
response = a.run
File.open('optimizedPdf.pdf', 'wb') do |f|
f.write(Base64.decode64(response.document.doc_data))
end