def save_pdf(topic_id: int, filename: str, with_comments: bool = False, project: str = 'h'): import logging logger = logging.getLogger('weasyprint') logger.handlers = [] # Remove the default stderr handler logger.addHandler(logging.FileHandler('pdf_weasyprint.log')) dir = os.path.dirname(filename) if dir != '' and not os.path.exists(dir): os.mkdir(dir) elif os.path.exists(filename): print("File {} is in target dir, skipping...".format(filename)) return if project == 'g': ht = GeektimesTopic(topic_id) else: ht = HabraTopic(topic_id) html = prepare_html(ht, with_comments=with_comments) css = CSS( string= '@page { size: A4; margin: 1cm; !important;} img { width: 100%; height: auto; !important; }' ) #css = CSS(string='@page { size: A4 landscape; margin: 1cm !important}') HTML(string=html).write_pdf(filename, stylesheets=[css])
def save_html(topic_id, filename, with_comments=False, project='h'): dir = os.path.dirname(filename) dir_imgs = filename + '.files' if dir != '' and not os.path.exists(dir): os.mkdir(dir) if not os.path.exists(dir_imgs): os.mkdir(dir_imgs) with open(filename, "wt") as f: if project == 'g': ht = GeektimesTopic(topic_id) else: ht = HabraTopic(topic_id) print('comments_cnt=', ht.comments_count()) html = prepare_html(ht, with_comments=with_comments) f.write(html)
def save_pdf(topic_id, filename, with_comments=False, project='h'): import logging logger = logging.getLogger('weasyprint') logger.handlers = [] # Remove the default stderr handler logger.addHandler(logging.FileHandler('pdf_weasyprint.log')) dir = os.path.dirname(filename) if dir != '' and not os.path.exists(dir): os.mkdir(dir) if project == 'g': ht = GeektimesTopic(topic_id) elif project == 'm': ht = MegamozgTopic(topic_id) else: ht = HabraTopic(topic_id) html = prepare_html(ht, with_comments=with_comments) css = CSS(string='@page { size: A4; margin: 1cm !important}') HTML(string=html).write_pdf(filename, stylesheets=[css])