Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added PDF-rotation-API/1.pdf
Binary file not shown.
49 changes: 49 additions & 0 deletions PDF-rotation-API/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Rotate PDF Endpoint

This API endpoint allows you to rotate a specific page of a PDF file. It uses the PyPDF2 library to perform the rotation.

## API Endpoint

### Endpoint URL

```
POST /rotate_pdf
```

### Request Parameters

- `details`: An object containing the rotation details.
- `page` (integer): The page number to rotate.
- `degree` (integer): The rotation angle in degrees.

- `file`: The PDF file to rotate.

### Response

The API response will include the following:

- `response` (string): A message indicating whether the PDF rotation was successful.

- `path` (string): The path of the rotated PDF file.

### Implementation Steps

1. Read the input rotation details and the PDF file.

2. Use the PyPDF2 library to open the PDF file.

3. Create a new PDF writer object.

4. Get the specified page from the PDF.

5. Rotate the page using the `rotateClockwise` method and the specified rotation degree.

6. Add the rotated page to the PDF writer.

7. Write the output PDF file using the PDF writer.

8. Close the output file.

9. Return the API response with the message indicating the success of the rotation and the path of the rotated PDF file.

That's it! With these implementation steps, you can rotate a specific page of a PDF file using this API endpoint.
22 changes: 22 additions & 0 deletions PDF-rotation-API/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from ninja import Form, NinjaAPI, File, Schema
from ninja.files import UploadedFile
from PyPDF2 import PdfFileReader,PdfFileWriter
import os
api=NinjaAPI()

class Inputfeild(Schema):
page: int
degree: int

@api.post("/rotate_pdf")
def rotate_pdf(request,details:Inputfeild = Form(...), file: UploadedFile = File(...)):
pdf = PdfFileReader(file)
writer = PdfFileWriter()
page = pdf.getPage(details.page)
page.rotateClockwise(details.degree)
writer.addPage(page)
output_file = open('final.pdf', 'wb')
writer.write(output_file)
path = os.path.realpath(output_file.name)
output_file.close()
return {'response':'pdf rotation successful',"path": path}
Binary file added PDF-rotation-API/final.pdf
Binary file not shown.