1+ import boto3
2+ import io
3+ from fastapi import APIRouter
4+ import os
5+ import requests
6+ from fastapi .responses import FileResponse
7+
8+ app = APIRouter ()
9+
10+ # S3 credentials
11+ AWS_ACCESS_KEY_ID = 'AKIAZTHHIOR4JJ5HLTUB'
12+ AWS_SECRET_ACCESS_KEY = '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'
13+ S3_BUCKET_NAME = 'learnmateai'
14+ S3_FOLDER_NAME = 'Sorted_PYQS/'
15+
16+ # Initialize S3 client
17+ s3_client = boto3 .client (
18+ 's3' ,
19+ aws_access_key_id = AWS_ACCESS_KEY_ID ,
20+ aws_secret_access_key = AWS_SECRET_ACCESS_KEY
21+ )
22+
23+ PDFSHIFT_API_KEY = 'c3fc79849a954be4bf9c719cdd3fee52'
24+
25+ @app .get ("/generate_pdf" )
26+ def generate_pdf ():
27+ # Retrieve all text files from the S3 folder
28+ response = s3_client .list_objects_v2 (
29+ Bucket = S3_BUCKET_NAME ,
30+ Prefix = S3_FOLDER_NAME
31+ )
32+
33+ file_names = [obj ['Key' ] for obj in response ['Contents' ] if obj ['Key' ].endswith ('.txt' )]
34+
35+ # Generate HTML content
36+ content = ""
37+
38+ for i ,file_name in enumerate (file_names , start = 1 ):
39+ # Retrieve text content from each text file
40+ obj = s3_client .get_object (Bucket = S3_BUCKET_NAME , Key = file_name )
41+ text_content = obj ['Body' ].read ().decode ('utf-8' )
42+
43+ # Replace newline characters with <br> tags
44+ text_content = text_content .replace ("\n " , "<br>" )
45+
46+ # Define the CSS class and color for the container
47+ css_class = f"box box-{ i % 2 + 1 } " # Alternate between box-1 and box-2
48+ color = "lightblue" if i % 2 == 1 else "lightgreen"
49+
50+ # Add heading and content to the HTML
51+ content += f"<div class='{ css_class } '>"
52+ content += f"<h2>Module { i } </h2>"
53+ content += f"<p>{ text_content } </p>"
54+ content += "</div>"
55+
56+ # Prepare the HTML payload
57+ html_payload = f"""
58+ <html>
59+ <head>
60+ <style>
61+ .box {{
62+ margin: 10px;
63+ padding: 10px;
64+ font-family: Arial, sans-serif;
65+ font-size: 12px;
66+ }}
67+ .box-1 {{
68+ background-color: lightblue;
69+ }}
70+ .box-2 {{
71+ background-color: lightgreen;
72+ }}
73+ </style>
74+ </head>
75+ <body>
76+ { content }
77+ </body>
78+ </html>
79+ """
80+
81+ # Convert HTML to PDF using PDFShift API
82+ response = requests .post (
83+ 'https://api.pdfshift.io/v3/convert/pdf' ,
84+ auth = ('api' ,PDFSHIFT_API_KEY ),
85+ json = {'source' : html_payload ,"landscape" : False , "use_print" : False }
86+ )
87+
88+ response .raise_for_status ()
89+
90+ # Save PDF response to a temporary file
91+ temp_pdf_path = "temp_pdf/temp_pdf.pdf" # Replace with the desired path to save the temporary PDF file
92+ with open (temp_pdf_path , "wb" ) as temp_pdf_file :
93+ for chunk in response .iter_content (chunk_size = 8192 ):
94+ temp_pdf_file .write (chunk )
95+
96+ # Return the temporary PDF file
97+ return FileResponse (temp_pdf_path , filename = 'combined_pdf.pdf' , media_type = 'application/pdf' )
0 commit comments