Exemplo n.º 1
0
 def generate_page_diff(self, orig_page_num, new_page_num):
     orig_page = self.orig_pdf.pdf_for_page(orig_page_num)
     new_page = self.new_pdf.pdf_for_page(new_page_num)
     if orig_page and new_page:
         changes = pdf_diff.compute_changes(
             orig_page, new_page)
         pprint(changes)
Exemplo n.º 2
0
def pdf_diff(response: Response,
             prev: UploadFile = File(...),
             current: UploadFile = File(...),
             img: Optional[bool] = True):
    diff_id = str(uuid.uuid4())
    working_dir = BASE_WORKING_DIR + "/" + diff_id + "/"
    # Create a new directory structure for each request to store the uploaded files and diff.pdf
    os.makedirs(working_dir)
    prev_path = copy_file(working_dir, prev, 'prev.pdf')
    current_path = copy_file(working_dir, current, 'current.pdf')
    changes = command_line.compute_changes(prev_path, current_path)

    json_path = working_dir + "/" + DIFF_JSON
    import json
    with open(json_path, 'w') as fp:
        json.dump(changes, fp)

    pdf_path = working_dir + "/" + DIFF_PDF
    render_changes(changes, pdf_path)
    if img:
        custom_headers = {
            DIFF_ID_HEADER: diff_id,
            'access-control-expose-headers': DIFF_ID_HEADER
        }
        return FileResponse(pdf_path,
                            media_type="application/pdf",
                            headers=custom_headers,
                            filename='diff.pdf')
    else:
        response.headers['access-control-expose-headers'] = DIFF_ID_HEADER
        response.headers[DIFF_ID_HEADER] = diff_id
        return changes
Exemplo n.º 3
0
    def generate_matching_sections(self):
        """Return a generator of MatchingSection.

        At most one MatchingSection is returned for each section in orig_pdf.
        """
        for sec in self.orig_pdf.comparable_sections:
            matching = get_section_range_pairs(sec, self.new_pdf)
            if not matching:
                continue
            matching.orig_range["dom"] = self.orig_pdf.dom
            matching.new_range["dom"] = self.new_pdf.dom
            changes = pdf_diff.compute_changes(
                matching.orig_range, matching.new_range, bottom_margin=93)
            if changes:
                matching.changes = changes
                yield matching
            else:
                print("No changes in", matching)
Exemplo n.º 4
0
def assertFileEqual(generated_data, master_filename):
    from pdf_diff.command_line import compute_changes
    import pkg_resources
    import tempfile
    import os
    import pytest
    master_file = pkg_resources.resource_filename(
        'sw.allotmentclub.browser.tests', master_filename)
    handle, generated_file = tempfile.mkstemp(suffix='pdf')
    os.fdopen(handle, 'wb').write(generated_data)
    changes = compute_changes(master_file, generated_file)
    if changes:
        changes_text = ""
        for change in changes:
            if change == '*':
                changes_text += '\n\r'
                continue
            changes_text += '{}: {}\n\r'.format(change['pdf']['file'],
                                                change['text'])
        pytest.fail('Generated pdf does not equal master: \n\r\n\r{}'.format(
            changes_text))
    else:
        os.remove(generated_file)