1

I am trying to understand a traceback error. See below.

What im doing is Manipulating PDF Form Fields with Python. Im using pdftk and fdfgen, just Manipulating PDF Form Fields with Python im following this example http://evanfredericksen.blogspot.mx/2014/03/manipulating-pdf-form-fields-with-python.html

Traceback:

File "/home/myproject/webapps/app/lib/python2.7/Django-1.11.7-py2.7.egg/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/home/myproject/webapps/app/lib/python2.7/Django-1.11.7-py2.7.egg/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/home/myproject/webapps/app/lib/python2.7/Django-1.11.7-py2.7.egg/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/myproject/webapps/app/app/home/views.py" in main
  113.             fields = get_fields(pdf_path)

File "/home/myproject/webapps/app/app/home/views.py" in get_fields
  67.         data_string = check_output(call).decode('utf8')

File "/usr/lib64/python2.7/subprocess.py" in check_output
  568.     process = Popen(stdout=PIPE, *popenargs, **kwargs)

File "/usr/lib64/python2.7/subprocess.py" in __init__
  711.                                 errread, errwrite)

File "/usr/lib64/python2.7/subprocess.py" in _execute_child
  1327.                 raise child_exception

Exception Type: OSError at /pdf_test/1/
Exception Value: [Errno 2] No such file or directory

My views code is:

def combine_pdfs(list_of_pdfs, outfile):
    '''
    Use pdftk to combine multiple pdfs into a signle pdf
    '''
    call = ['pdftk']
    call += list_of_pdfs
    call += ['cat', 'output']
    call += [outfile]
    print(" ".join(call))
    try:
        data_string = check_output(call).decode('utf8')
    except IOError:
        raise PdftkNotInstalledError('Could not locate PDFtk installation')
    return outfile

def get_fields(pdf_file):
    '''
    Use pdftk to get a pdf's fields as a string, parse the string
    and return the fields as a dictionary, with field names as keys
    and field values as values.
    '''
    fields = {}
    call = ['pdftk', pdf_file, 'dump_data_fields_utf8']
    try:
        data_string = check_output(call).decode('utf8')
    except IOError:
        raise PdftkNotInstalledError('Could not locate PDFtk installation')
    data_list = data_string.split('\r\n')
    if len(data_list) == 1:
        data_list = data_string.split('\n')
    for line in data_list:
        if line:
            re_object = match(r'(\w+): (.+)', line)
            if re_object is not None:
                if re_object.group(1) == 'FieldName':
                    key = re_object.group(2)
                    fields[key] = ''
                elif re_object.group(1) == 'FieldValue':
                    fields[key] = re_object.group(2)
    return fields

def write_pdf(source, fields, output, flatten=False):
    '''
    Take a source file path, list or dictionary of fdf fields, and
    output path, and create a filled-out pdf.
    '''
    fdf = forge_fdf(fdf_data_strings=fields)
    with NamedTemporaryFile(delete=False) as file:
        file.write(fdf)
    call = ['pdftk', source, 'fill_form', file.name, 'output', output]
    if flatten:
        call.append('flatten')
    try:
        check_output(call)
    except IOError:
        raise PdftkNotInstalledError('Could not locate PDFtk installation')
    remove(file.name)

class PdftkNotInstalledError(Exception):
    pass


def main(request,pk):
    if pk:
        obj = MyModel.objects.get(pk=pk)
        if obj.text == 'Secure':

            pdf_path = "/home/myproject/webapps/app/app/home/templates/home/fill.pdf"

            fields = get_fields(pdf_path)

            fields['F[0].P1[0].Name[0]'] = obj.name

            write_pdf(pdf_path,fields,'output.pdf')

        return HttpResponseRedirect('/Search')

urls.py is :

url(r'^pdf_test/(?P<pk>[-\w]+)/$', views.main, name='pdf_test')
J.Smith
  • 11
  • 1

0 Answers0