<commit_before># coding: utf-8

import re
import subprocess
import uuid
from tempfile import NamedTemporaryFile

try:
    from urllib.request import pathname2url
    from urllib.parse import urljoin
except ImportError:  # Python2
    from urllib import pathname2url
    from urlparse import urljoin

import django
from django.conf import settings
from django.core.files import File
from django.template import Context, RequestContext
from django.utils import six
from django.utils.encoding import smart_text


def electron_pdf(input, output_file=None, **kwargs):
    """
    Converts html to PDF using https://github.com/fraserxu/electron-pdf.
    input: input file or url of the html to be converted.
    output_file: Optional output file path. If None, the output is returned.
    **kwargs: Passed to electron-pdf via options

    example usage:
        electron_pdf(
            input='/tmp/example.html',
            landscape=True,
            printBackground=True,
            pageSize='A4'
        )
    """
    # Default options:
    options = getattr(settings, 'ELECTRON_PDF_OPTIONS', {})

    if not output_file:
        output_file = '/tmp/{}.pdf'.format(uuid.uuid4())

    subprocess.call('electron-pdf {} {}'.format(input.filename, output_file), shell=True)

    with open(output_file, 'rb') as f:
        return File(f).read()


class RenderedFile(object):
    """
    Create a temporary file resource of the rendered template with context.
    The filename will be used for later conversion to PDF.
    """
    temporary_file = None
    filename = ''

    def __init__(self, template, context, request=None):
        self.temporary_file = render_to_temporary_file(
            template=template,
            context=context,
            request=request,
            prefix='electron_pdf', suffix='.html',<commit_msg>add option to run without graphical env<commit_after># coding: utf-8

import re
import subprocess
import uuid
from tempfile import NamedTemporaryFile

try:
    from urllib.request import pathname2url
    from urllib.parse import urljoin
except ImportError:  # Python2
    from urllib import pathname2url
    from urlparse import urljoin

import django
from django.conf import settings
from django.core.files import File
from django.template import Context, RequestContext
from django.utils import six
from django.utils.encoding import smart_text


def electron_pdf(input, output_file=None, **kwargs):
    """
    Converts html to PDF using https://github.com/fraserxu/electron-pdf.
    input: input file or url of the html to be converted.
    output_file: Optional output file path. If None, the output is returned.
    **kwargs: Passed to electron-pdf via options

    example usage:
        electron_pdf(
            input='/tmp/example.html',
            landscape=True,
            printBackground=True,
            pageSize='A4'
        )
    """
    # Default options:
    options = getattr(settings, 'ELECTRON_PDF_OPTIONS', {})

    if not output_file:
        output_file = '/tmp/{}.pdf'.format(uuid.uuid4())

    if settings.ELECTRON_WITHOUT_GRAPHICAL_ENV:
        subprocess.call('xvfb-run --server-args "-screen 0 1024x768x24" electron-pdf {} {}'.format(input.filename, output_file), shell=True)
    else:
        subprocess.call('electron-pdf {} {}'.format(input.filename, output_file), shell=True)

    with open(output_file, 'rb') as f:
        return File(f).read()


class RenderedFile(object):
    """
    Create a temporary file resource of the rendered template with context.
    The filename will be used for later conversion to PDF.
    """
    temporary_file = None
    filename = ''

    def __init__(self, template, context, request=None):
        self.temporary_file = render_to_temporary_file(
            template=template,
            context=context,
            request=request,
            prefix='electron_pdf', suffix='.html',
{'fraction_non_alphanumeric': 0.09020817270624518, 'fraction_numerical': 0.005654073502955538, 'mean_word_length': 3.071129707112971, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1738998', 'n_tokens_mistral': 1114, 'n_tokens_neox': 1083, 'n_words': 337}