了解回溯错误[DJANGO / PYTHON]

时间:2017-11-12 01:15:59

标签: python django subprocess pdftk fdfgen

我正在尝试了解回溯错误。见下文。

我正在做的是使用Python操作PDF表单字段。 我正在使用pdftk和fdfgen,只是使用Python操作PDF表单字段我遵循此示例http://evanfredericksen.blogspot.mx/2014/03/manipulating-pdf-form-fields-with-python.html

import React, { Component } from "react";
import Clock from "./Clock";
import "./App.css";

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
     deadline: "December 25, 2017",
     newDeadline: ""
   };
 }

  changeDeadline() {
   this.setState({ deadline: this.state.newDeadline });
  }

  render() {
    return (
      <div className="App">
        <div className="App-title">Countdown to {this.state.deadline}
          </div>
           <div>
            <Clock dealine={this.state.deadline} />
           </div>
         <div>
          <input
           placeholder="new date"
            onChange={event =>
             this.setState({ newDeadline: event.target.value })}
         />
         <button onClick={() => this.changeDeadline()}>Submit</button>
       </div>
     </div>
   );
 }
}

export default App;

我的观点代码是:

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

urls.py是:

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')

0 个答案:

没有答案