How to Make a Searchable PDF in JavaScript
In this tutorial, we will learn how to make a searchable PDF using PDF.co Web API in JavaScript.
Let's explore the code. We're working with a PDF file that contains an image or a scanned page, and our goal is to create searchable text from it.
Step 1: Declare the Pages to be Processed
To start, we set up placeholders for API keys that will be included in the request header. We also establish a variable for the source file, which identifies the pages to be processed.
If a page is protected, a password placeholder is required. Since PDF.co supports multiple languages such as English, French, and Spanish, and our input file is in English, we will specify "ENG" for the language. Lastly, we will define the destination file name.

Step 2: Get the Placeholder URL for the PDF
We start by creating a web client object and providing the header keys. We have the physical file, and to complete the process, we need a URL.
So we will obtain a pre-signed URL, which serves as a temporary placeholder for the PDF file we intend to upload. After acquiring the pre-signed URL, we then provide the source file.

Step 3: Process the Request
Simply put, we will input the file name and initiate the request. Upon receiving a response, we use the pre-signed URL to upload the actual file. Following the upload, we process the file to make it searchable. We generate a URL for creating the searchable PDF and include parameters such as name, password, pages, language, and the URL.
Parse the API response as JSON and check for an error. JSON parsing reads the returned data; it does not optimize the PDF. If the request starts an asynchronous job, retain its job ID and wait for success before downloading the searchable PDF.

Step 4: Prepare the URL for the “Make Searchable PDF” API Call
The upload-preparation response provides two different URLs:
presignedUrl: Send the source file’s bytes to this URL using a PUT request.url: After the upload succeeds, use this URL as the source document for the searchable-PDF request.
Send the conversion request to /v1/pdf/makesearchable. Include the source url, output name, PDF password if required, pages and OCR language through lang. Leave pages empty to process all pages in this example, and use "eng" for English.
Step 5: Asynchronous Option
We have an asynchronous option available for processing, which is particularly useful for large files where waiting for a synchronous response might be impractical. In asynchronous mode, we can submit the job and proceed without waiting for the immediate completion of the process.
Retain the returned jobId and check it with POST /v1/job/check. Wait between checks while status is working and set an application waiting deadline. Download the searchable PDF only after success. Stop on failed, aborted or a job-not-found response. Reaching the deadline does not cancel the job, so retain its ID before deciding whether to check again later.
Step 6: Result
The outcome is that the file, while retaining its original layout, is now searchable, effectively transforming it from a scanned version to a text-selectable one. Users can select, copy, and paste text from the document as needed.

For those interested in learning more about this functionality and exploring further capabilities, the API documentation provides a wealth of information and guidance.
GET YOUR API KEYZAPIER PLUGINEXPLORE API DOCSRelated Tutorials
