Skip to content

Commit 3747bc0

Browse files
Merge pull request avinashkranjan#1816 from alwenpy/master
PDF rotation API script
2 parents ccb58a6 + 78d8111 commit 3747bc0

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

PDF-rotation-API/1.pdf

2.96 KB
Binary file not shown.

PDF-rotation-API/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Rotate PDF Endpoint
2+
3+
This API endpoint allows you to rotate a specific page of a PDF file. It uses the PyPDF2 library to perform the rotation.
4+
5+
## API Endpoint
6+
7+
### Endpoint URL
8+
9+
```
10+
POST /rotate_pdf
11+
```
12+
13+
### Request Parameters
14+
15+
- `details`: An object containing the rotation details.
16+
- `page` (integer): The page number to rotate.
17+
- `degree` (integer): The rotation angle in degrees.
18+
19+
- `file`: The PDF file to rotate.
20+
21+
### Response
22+
23+
The API response will include the following:
24+
25+
- `response` (string): A message indicating whether the PDF rotation was successful.
26+
27+
- `path` (string): The path of the rotated PDF file.
28+
29+
### Implementation Steps
30+
31+
1. Read the input rotation details and the PDF file.
32+
33+
2. Use the PyPDF2 library to open the PDF file.
34+
35+
3. Create a new PDF writer object.
36+
37+
4. Get the specified page from the PDF.
38+
39+
5. Rotate the page using the `rotateClockwise` method and the specified rotation degree.
40+
41+
6. Add the rotated page to the PDF writer.
42+
43+
7. Write the output PDF file using the PDF writer.
44+
45+
8. Close the output file.
46+
47+
9. Return the API response with the message indicating the success of the rotation and the path of the rotated PDF file.
48+
49+
That's it! With these implementation steps, you can rotate a specific page of a PDF file using this API endpoint.

PDF-rotation-API/api.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from ninja import Form, NinjaAPI, File, Schema
2+
from ninja.files import UploadedFile
3+
from PyPDF2 import PdfFileReader,PdfFileWriter
4+
import os
5+
api=NinjaAPI()
6+
7+
class Inputfeild(Schema):
8+
page: int
9+
degree: int
10+
11+
@api.post("/rotate_pdf")
12+
def rotate_pdf(request,details:Inputfeild = Form(...), file: UploadedFile = File(...)):
13+
pdf = PdfFileReader(file)
14+
writer = PdfFileWriter()
15+
page = pdf.getPage(details.page)
16+
page.rotateClockwise(details.degree)
17+
writer.addPage(page)
18+
output_file = open('final.pdf', 'wb')
19+
writer.write(output_file)
20+
path = os.path.realpath(output_file.name)
21+
output_file.close()
22+
return {'response':'pdf rotation successful',"path": path}

PDF-rotation-API/final.pdf

1.38 KB
Binary file not shown.

0 commit comments

Comments
 (0)