summaryrefslogtreecommitdiff
path: root/megapixels/commands/visualize/weasyprinter.py
blob: 4e9cd1ac46392b9fb589290aee078fa1337eb52e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Crop images to prepare for training
"""

import click
# from PIL import Image, ImageOps, ImageFilter, ImageDraw

from app.settings import types
from app.utils import click_utils
from app.settings import app_cfg as cfg

color_filters = {'color': 1, 'gray': 2, 'all': 3}

@click.command()
@click.option('--html', 'opt_fp_in_html', required=True,
  help='Input HTML')
@click.option('--css', 'opt_fp_in_css', required=True,
  help='Input CSS')
@click.option('-o', '--output', 'opt_fp_out', required=True,
  help='Output PDF')
@click.option('-f', '--force', 'opt_force', is_flag=True,
  help='Force overwrite file')
@click.pass_context
def cli(ctx, opt_fp_in_html, opt_fp_in_css, opt_fp_out, opt_force):
  """Generates PDF files from HTML, CSS"""
  
  import sys
  import os
  from os.path import join
  from pathlib import Path
  
  from tqdm import tqdm
  import numpy as np
  import pandas as pd
  from weasyprint import HTML, CSS
  from weasyprint.fonts import FontConfiguration

  from app.utils import logger_utils, file_utils, im_utils, display_utils, draw_utils
  from app.utils import plot_utils
  from app.models.data_store import DataStore

  # -------------------------------------------------
  # init here

  log = logger_utils.Logger.getLogger()


  font_config = FontConfiguration()
  html = HTML(filename=opt_fp_in_html)
  css  = CSS(filename=opt_fp_in_css)
  document = html.render(stylesheets=[css], font_config=font_config)
  document.resolve_links()
  document.make_bookmark_tree()
  document.write_pdf(opt_fp_out)