import fitz # PyMuPDF import io from weasyprint import HTML, CSS from bs4 import BeautifulSoup import sys css=''' body { font-size: 40px; line-height: 40px; color: #FFF; font-family: "Times New Roman", Times, serif; whitespace: pre-wrap; } @page { /* Set specific page dimensions: width height */ size: 1920px 1080px; margin: 70px; background-color: #2c5aa0; color: white; whitespace: pre-wrap; } ''' #html_skeleton = "

{}

".format(sys.stdin.read().replace('\n','
').replace('\t',' ')) html_skeleton = "

{}

".format(sys.stdin.read()) HTML(string=html_skeleton).write_pdf('output.pdf', stylesheets=[CSS(string=css)]) pdf_bytes=HTML(string=html_skeleton).write_pdf(stylesheets=[CSS(string=css)])