Exemplo n.º 1
0
    def on_config(self, config):

        if 'enabled_if_env' in self.config:
            env_name = self.config['enabled_if_env']
            if env_name:
                self.enabled = os.environ.get(env_name) == '1'
                if not self.enabled:
                    self._logger.warning(
                        'without generate PDF'
                        f'(set environment variable {env_name} to 1 to enable)'
                    )
                    return
            else:
                self.enabled = True
        else:
            self.enabled = True

        self._options = Options(self.config, config, self._logger)

        from weasyprint.logger import LOGGER
        if self._options.verbose:
            LOGGER.setLevel(logging.DEBUG)
            self._logger.setLevel(logging.DEBUG)
        else:
            LOGGER.setLevel(logging.ERROR)

        if self._options.strict:
            self._error_counter = _ErrorAndWarningCountFilter()
            LOGGER.addFilter(self._error_counter)
            self._logger.addFilter(self._error_counter)

        self.generator = Generator(options=self._options)
        '''
    def on_config(self, config):
        if 'enabled_if_env' in self.config:
            env_name = self.config['enabled_if_env']
            if env_name:
                self.enabled = os.environ.get(env_name) == '1'
                if not self.enabled:
                    print(
                        'PDF export is disabled (set environment variable {} to 1 to enable)'
                        .format(env_name))
                    return

        self.combined = self.config['combined']
        if self.combined:
            print('Combined PDF export is enabled')

        from .renderer import Renderer
        self.renderer = Renderer(self.combined, config['theme'].name,
                                 self.config['theme_handler_path'])

        from weasyprint.logger import LOGGER
        import logging

        if self.config['verbose']:
            LOGGER.setLevel(logging.DEBUG)
        else:
            LOGGER.setLevel(logging.ERROR)

        handler = logging.StreamHandler()
        handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
        LOGGER.addHandler(handler)
Exemplo n.º 3
0
 def evil_error(msg, *args, **kwargs):
     if msg.startswith('Failed to load image'):
         raise WeasyprintError(msg % tuple(args))
     else:
         return weasyprint_logs.log(logging.ERROR, msg, *args, **kwargs)
Exemplo n.º 4
0
 def evil_error(msg, *args, **kwargs):
     if msg.startswith('Failed to load image'):
         application.logger.exception(msg % tuple(args))
         abort(502)
     else:
         return weasyprint_logs.log(logging.ERROR, msg, *args, **kwargs)
Exemplo n.º 5
0
def render(cat_image, background_image, text):
    ugly_text = uglify(text)
    font = random.choice(fonts)
    color = random.choice(colors)
    pixabay_logo = os.path.join(os.getcwd(), 'spreukbot/pixabay-logo.png')
    cat_url, cat_width, cat_height = cat_image
    bg_url, bg_width, bg_height = background_image
    demo_page = f'''
        data:text/html,
        <!doctype html>
        <html>
        <head>
            <meta charset="utf-8">
            <title>LOL</title>
            <link href="https://fonts.googleapis.com/css?family=Pacifico|Baloo+Tamma|Merriweather|Poiret+One" rel="stylesheet">
            <style>
                @page {{
                    margin: 0;
                    size: {cat_width * 3}px {cat_height}px;
                }}
                * {{
                    padding: 0;
                    margin: 0;
                    text-shadow: #FC0 1px 0 10px;
                }}
                html, body {{
                    text-align: center;
                    padding: 0;
                }}
                body:before {{
                    content: "";
                    position: fixed;
                    top: 0;
                    left: 0;
                    width: 100%;
                    height: 100%;
                    background-size: cover;
                    background-image: url('{bg_url.replace('https://pixabay.com', 'http://spreukbot-pixabay.barkr.uk')}');
                    background-repeat: no-repeat;
                }}
                p {{
                    position: absolute;
                    top: 0;
                    left: 0;
                    width: 100%;
                    height: 100%;
                    font-family: {font};
                    font-size: 32px;
                    color: {color};
                    text-stroke: 2px white;
                }}
                p.shadow {{
                    color: black !important;
                    top: 1px;
                    left: 1px;
                }}
                div.watermark {{
                    position: absolute;
                    right: 10%;
                    bottom: 10%;
                    transform: rotate(-45deg);
                    opacity: .2;
                    width: 20%;
                }}
                img.pixabay {{
                    position: absolute;
                    right: 0;
                    top: 0;
                }}
            </style>
        </head>
        <body>
        <p>text</p>
        <div class="watermark">
            Wijze spreuken om te delen van Marko V. Keten
        </div
        <img class="pixabay" src="cat_url">
        </body>
        </html>
    '''
    from weasyprint import HTML
    from weasyprint.logger import LOGGER as weasyprint_logger
    import logging
    logging.getLogger('weasyprint').setLevel(logging.DEBUG)
    weasyprint_logger.addFilter(WeasyprintLoggerFilter())
    weasyprint_logger.setLevel(logging.DEBUG)
    data = HTML(string=demo_page).write_png()
    return data
Exemplo n.º 6
0
def render(image_url, image_width, image_height, text, emoji=""):
    emoji = ""
    ugly_text = uglify(text)
    font = random.choice(fonts)
    color = random.choice(colors)
    pixabay_logo = os.path.join(os.getcwd(), 'spreukbot/pixabay-logo.png')
    emoji_font_path = os.path.join(package_directory,
                                   'fonts/noto/NotoColorEmoji.woff')
    demo_page = f'''
        data:text/html,
        <!doctype html>
        <html>
        <head>
            <meta charset="utf-8">
            <title>LOL</title>
            <link href="https://fonts.googleapis.com/css?family=Pacifico|Baloo+Tamma|Merriweather|Poiret+One|Noto+Sans" rel="stylesheet">
            <style>
                @font-face {{
                    font-family: 'Noto Emoji';
                    font-style: normal;
                    font-weight: 300;
                    src: url('file://{emoji_font_path}') format('ttf');
                }}
                @page {{
                    margin: 0;
                    size: {image_width}px {image_height}px;
                }}
                * {{
                    padding: 0;
                    margin: 0;
                    text-shadow: #FC0 1px 0 10px;
                }}
                html, body {{
                    text-align: center;
                    padding: 0;
                    height: 100%;
                }}
                body:before {{
                    content: "";
                    position: fixed;
                    top: 0;
                    left: 0;
                    width: 100%;
                    height: 100%;
                    background-size: cover;
                    background-image: url({image_url});
                    background-repeat: no-repeat;
                }}
                p.saying {{
                    position: absolute;
                    top: 0;
                    left: 0;
                    width: 100%;
                    height: 100%;
                    font-family: {font};
                    font-size: 32px;
                    color: {color};
                }}
                p.saying.shadow {{
                    color: black !important;
                    top: 1px;
                    left: 1px;
                }}
                p.emoji {{
                    position: absolute;
                    bottom: 10%;
                    left: 0;
                    width: 100%;
                    text-align: center;
                    font-family: 'Noto Emoji', sans-serif;
                    font-size: 54px;
                }}
                div.watermark {{
                    position: absolute;
                    right: 10%;
                    bottom: 10%;
                    transform: rotate(-45deg);
                    opacity: .2;
                    width: 20%;
                }}
                img.pixabay {{
                    position: absolute;
                    left: 0;
                    bottom: 0;
                }}
            </style>
        </head>
        <body>
        <p class="emoji">{emoji}</p>
        <p class="saying shadow">{ugly_text}</p>
        <p class="saying">{ugly_text}</p>
        <div class="watermark">
            Wijze spreuken voor te delen van Marko V. Keten
        </div
        <img class="pixabay" src="file://{pixabay_logo}">
        </body>
        </html>
    '''
    from weasyprint import HTML
    from weasyprint.logger import LOGGER as weasyprint_logger
    from weasyprint.fonts import FontConfiguration
    import logging
    font_config = FontConfiguration()
    logging.getLogger('weasyprint').setLevel(logging.DEBUG)
    weasyprint_logger.addFilter(WeasyprintLoggerFilter())
    weasyprint_logger.setLevel(logging.DEBUG)
    data = HTML(string=demo_page).write_png(font_config=font_config)
    return data