Created
May 30, 2024 15:18
-
-
Save apetenchea/4df556a49f9a2543be877c31355b4164 to your computer and use it in GitHub Desktop.
Download any manuals from https://www.manua.ls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script gathers all the pages of a manual and merges them into a PDF. | |
# You'll need to play a bit with inspect-element in order to figure out the format the correct url, | |
# but it should be easy to adapt it to any manual. | |
# This script is specifically for https://www.manua.ls/audi/q3-2018/manual. | |
# Their url format is https://www.manua.ls/viewer/{manual-id}/{page-number}/bg{page-number-hex}.png | |
# Example: https://www.manua.ls/viewer/668006/100/bg64.png | |
# Enjoy! | |
import requests | |
from tqdm import tqdm | |
from PIL import Image | |
from io import BytesIO | |
from reportlab.pdfgen import canvas | |
from reportlab.lib.pagesizes import letter | |
from reportlab.lib.utils import ImageReader | |
def download_image(url): | |
response = requests.get(url) | |
if response.status_code == 200: | |
return Image.open(BytesIO(response.content)) | |
else: | |
print(f"Failed to download {url}") | |
return None | |
def save_images_as_pdf(images, pdf_filename): | |
c = canvas.Canvas(pdf_filename, pagesize=letter) | |
width, height = letter | |
for image in images: | |
image_width, image_height = image.size | |
aspect_ratio = image_width / image_height | |
new_width = width | |
new_height = width / aspect_ratio | |
if new_height > height: | |
new_height = height | |
new_width = height * aspect_ratio | |
# Convert PIL image to byte stream | |
img_byte_arr = BytesIO() | |
image.save(img_byte_arr, format='PNG') | |
img_byte_arr.seek(0) | |
# Draw image from byte stream | |
c.drawImage(ImageReader(img_byte_arr), 0, height - new_height, width=new_width, height=new_height) | |
c.showPage() | |
c.save() | |
def main(): | |
base_url = "https://www.manua.ls/viewer/668006/" | |
images = [] | |
for i in tqdm(range(1, 231)): # Adjust the range as needed | |
url = f"{base_url}{i}/bg{hex(i)[2:]}.png" | |
image = download_image(url) | |
if image: | |
images.append(image) | |
if images: | |
save_images_as_pdf(images, "output.pdf") | |
print("PDF created successfully") | |
else: | |
print("No images downloaded") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment