Blog
Merge PDFs together with Python on AWS Lambda
Intro
Api2Pdf is a REST API that allows you to generate PDFs at massive scale. Among the functions that Api2Pdf provides, merging PDFs together is one of the more popular capabilities. It is quite simple to do in Python, and this article will show you how.
Merge PDF API
In order to merge PDFs together, you first have to install the Api2Pdf’s Python Client Library. Details on its full usage is available here. In this article we are just going to focus on combining PDFs together.
Run the pip command for installing the client:
pip install api2pdf
In your python code, import Api2Pdf and initialize the client library with your API key. Replace ‘YOUR-API-KEY’ with your own API key that you can acquire by creating an account on portal.api2pdf.com.
from api2pdf import Api2Pdf
a2p_client = Api2Pdf('YOUR-API-KEY')
Merge / Concatenate Two or More PDFs
To use the merge endpoint, supply a list of urls to existing PDFs. The engine will consume all of the PDFs and merge them into a single PDF, in the order in which they were provided in the list. It is essential that the urls to the PDFs are publicly accessible on the web.
Merge PDFs from list of URLs to existing PDFs
links_to_pdfs = ['https://LINK-TO-PDF', 'https://LINK-TO-PDF']
merge_result = a2p_client.merge(links_to_pdfs)
print(api_response.result)
That’s all there is to merging multiple PDFs together using Python. Api2Pdf has many more functions, such as converting HTML to PDF or a URL to PDF. You can view the documentation for all of the available endpoints at https://www.api2pdf.com/documentation. If you run into issues and require assistance from Api2Pdf Support, see this article on the best way to get support from us!
Merge PDFs on AWS Lambda
Reliability is essential when you are merging PDFs together. It is a CPU intensive operation. Using a serverless architecture like AWS Lambda or Azure Functions just makes perfect sense for this use-case. Api2Pdf merges PDFs on Lambda which allows us to scale to millions of requests at extremely low cost.