New: PDF to E-Invoice for Factur-X/ZUGFeRD, and PDF to PDF/A for archive-ready PDF/A-3b.

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

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 a longer conversion, use asynchronous processing where supported. Retain the returned job ID and check its status, allowing a delay between checks. Download the output only after success; stop on failed or aborted.

Async mode does not guarantee completion or eliminate network timeouts. Set request timeouts and a polling deadline so the application cannot wait indefinitely.

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: Test the Opening Password and Permissions

Open the generated PDF in a fresh viewer session. Confirm that opening it without a password is denied, then enter the configured userPassword.

While using the user password, test the permissions you configured, such as printing or copying, in a PDF reader that supports these restrictions.

The ownerPassword manages permissions and can bypass those restrictions. Use a different owner password and keep it private if document recipients should only have user-level access.

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