How to Rotate PDF Pages using Rest API in Python

Rotate PDF Pages using Rest API in Python

There can be many cases where you want to rotate pages of documents featuring the wrong orientation or contain disoriented pages. For example, if your PDF document pages are upside down, reading the document may be quite difficult. Rotating pages is a very useful help to improve your reader experience. So an easy solution to fix document rotation is using GroupDocs.Merger Python SDK. You can rotate all pages or specific pages of a PDF file programmatically using Python SDK. In this article, we will show you how to permanently rotate PDF file pages using REST API in Python.

The following are topics that will be discussed in this article:

PDF Pages Rotation Rest API and Python SDK

For rotating PDF files, I will be using the Python SDK of GroupDocs.Merger Cloud API. You can rotate PDF pages by setting rotation angles like 90, 180, or 270 degrees using GroupDocs.Merger API. It also allows you to split, combine, remove and rearrange a single page or a collection of pages from supported document formats of Word, Excel, PDF, PowerPoint, and HTML etc. You can install GroupDocs.Merger Cloud SDK to your Python application using the following command in the terminal:

You can install GroupDocs.Merger Python SDK into your Python application code using the following command in the console:

pip install groupdocs_merger_cloud

Please get your Client ID and Client Secret from the dashboard before you start following the steps and available code examples. Once you have your ID and secret, add in the code as demonstrated below:

Once the Cloud SDK is configured successfully, you can use the Rotation enumeration to select a suitable value of rotation in the clockwise direction.

How to Rotate All Pages in PDF File Online in Python

In the following section, you can permanently rotate PDF file pages in the cloud. Rotation is based on 90-degree increments. PDF pages can be rotated by 0/90/180/270 degrees. The following are the steps to rotate a PDF page. First, upload the PDF file to the cloud and the uploaded PDF file will be available in the files section of the dashboard on the cloud. There could be certain scenarios where you want to rotate PDF files. You can rotate all pages of PDF file by following the steps mentioned below:

  • Firstly, create an instance of the PagesApi class
  • Secondly, create an instance of the RotateOptions class
  • Then, create an instance of the FileInfo
  • Now, provide the input PDF document path and output file path
  • Next, set the desired page rotation like Rotate90
  • After that, create the RotateRequest with RotateOptions as an argument
  • Lastly, call the rotate() class and save the output PDF document

The following code snippet shows how to rotate all pages of a PDF file using REST API in Python:

Finally, the above code sample will save the updated PDF file on the cloud. You can download the rotated PDF document using the download file code snippet.

Rotate Specific Pages of PDF Document using Python

The rotation in a PDF document is applied at a page level. Therefore, you can also rotate specific pages of PDF file as per your requirements. You only need to choose the page number you want to apply the rotation on. The steps below explain how to rotate certain pages of PDF file:

  • Firstly, create an instance of the PagesApi
  • Secondly, create an instance of the RotateOptions class
  • Then, create an instance of the FileInfo class
  • Provide the input PDF document path and output file path
  • Assign the exact page numbers using pages collection
  • Set the desired page rotation to Rotate180
  • After that, create the RotateRequest with RotateOptions as an argument
  • Finally, call the rotate() method and save the output PDF document

The following code snippet elaborates on how to rotate specific or certain pages in a PDF document using Python:

Finally, the above code sample will save the output PDF file on the cloud.

Rotate PDF Pages by Page Number Range using Python

You can also rotate PDF pages by page number. You need to provide the start page number and end page number to apply the rotation. The steps below explain how to rotate PDF pages by page numbers of a PDF file:

  • Firstly, create an instance of the PagesApi
  • Next, create an instance of the RotateOptions class
  • Then, create an instance of the FileInfo class
  • Provide the input PDF document path and output file path
  • Set the desired page rotation like Rotate270
  • Set the start page number and end page number values;
  • Next, set the range_mode to EvenPages
  • After that, create the RotateRequest with RotateOptions as an argument
  • Finally, call the rotate() method and save the output PDF document

The code snippet below shows how to rotate pages in PDF document by providing page numbers using Python Rest API:

The above code example will save the output PDF document on the cloud.

Rotate PDF Pages Free Online

How to rotate PDF pages online for free? Please try the following PDF rotate free online tool to rotate PDF online free, which is developed using the above API.

Summing up

This brings us to the conclusion of this article. You learned about these materials in this article:

  • how to rotate all pages of PDF document using Python;
  • how to rotate specific pages of PDF file using Python;
  • how to rotate PDF pages by page range in Python;

Moreover, you can learn more about GroupDocs.Merge Cloud API using the documentation. We also have an API Reference section that lets you visualize and interact with our Cloud APIs directly through the browser. For other interesting topics, please stay in touch for regular updates. We suggest you follow our Getting Started guide.

Finally, groupdocs.cloud is currently writing new blog articles on different file format solutions using REST API. Stay tuned for the latest updates.

Ask a question

If you have any queries regarding the PDF page rotator online, please feel free to ask us at Free Support Forum

FAQs

How to rotate PDF documents permanently in Python?

Please visit this link to learn the Python code snippet for how to rotate PDF file permanently in Python.

How to rotate PDF file online using REST API?

Create an instance of PagesApi, set the values of the RotateOptions, and invoke the pagesApi.rotate() method with RotateRequest to rotate PDF and save it online in Python.

How to install PDF page rotator free download library?

You can install PDF rotator free download Python library to rotate PDF in windows programmatically.

How do I rotate PDF pages in windows?

Please visit this link to download the PDF pages rotator for free. This offline software is used to perform different file format operations, including document rotation in windows, using a single click.

See Also