File tree Expand file tree Collapse file tree 4 files changed +71
-0
lines changed Expand file tree Collapse file tree 4 files changed +71
-0
lines changed Original file line number Diff line number Diff line change
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.
Original file line number Diff line number Diff line change
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 }
You can’t perform that action at this time.
0 commit comments