Convert DOC to Protected PDF in Async with PHP using PDF.co Web API

Nov 26, 2024·2 Minutes Read

In this tutorial, we’ll guide you through the steps to convert a Doc/Docx file into a protected PDF using the PDF.co Web API with PHP. A sample Word document will be used to demonstrate the process.

Sample Word Document
Sample Word Document

Step 1: Copy and Review the Source Code

  • Begin by copying the provided HTML and PHP source code.
  • Paste this code into your preferred code editor, such as VS Code, Sublime Text, PHPStorm, or any other editor you prefer.
  • Please ensure that the Owner and User password values are added, the PDF permissions are set, and the security settings are properly configured.
HTML and PHP Source Code

For larger files, enable asynchronous processing. This allows the job to run in the background and track progress without timeouts. Refer to PDF.co's documentation for more details on asynchronous jobs.

Step 2: Save the PHP and HTML File in the Directory

Once you've edited the code, save the PHP and HTML file in the appropriate directory for local hosting.

  • For XAMPP Users: Save the file in the /htdocs directory.
  • For WampServer Users: Save the file in the /www directory.

Saving the file in these locations ensures that it’s accessible from your local server.

Save PHP File

Step 3: Start the Apache Module

To execute your PHP script, ensure that your Apache server is running.

  • Open the XAMPP or WampServer control panel.
  • Click Start next to the Apache module to activate the server.
Start the Apache Module

Step 4: Run the PHP Program Locally

Open your browser and enter the following in the address bar to access the file:

  • URL Path: If you saved the file as sample.html in a folder, enter localhost/your-folder-name/sample.html.
  • Add Your API Key: Log in to your PDF.co account and retrieve your API Key from your dashboard.
  • Input Your DOC/DOCX File: Upload the DOC or DOCX file you want to convert to PDF.
  • Send the Request: Click the Proceed button to send the request to the PDF.co Web API for conversion.
Run the PHP Program Locally

Step 5: Access the Generated PDF Result

Once the process is complete, a link to the output file will be generated. Click on the link to open the protected PDF file.

Access the Generated PDF Result

Step 6: Enter the Security Password

To access the file, you need to enter the security password. This password ensures that only authorized users can open and view the file.

Enter the Security Password

Step 7: Generated Protected PDF

Here is the protected PDF file generated from the Word document.

Generated Protected PDF

In this tutorial, you learned how to convert a DOC file into a protected PDF in PHP using PDF.co Web API. You also learned how easy it is to set up the code samples to get your program running right away.

Related Tutorials

See Related Tutorials