def rewrite_pdf(file_data, *, page_count, allow_international_letters, filename): file_data, recipient_address, redaction_failed_message = rewrite_address_block( file_data, page_count=page_count, allow_international_letters=allow_international_letters, ) if not does_pdf_contain_cmyk(file_data) or does_pdf_contain_rgb(file_data): file_data = convert_pdf_to_cmyk(file_data) if contains_unembedded_fonts(file_data): file_data = remove_embedded_fonts(file_data) if contains_unembedded_fonts(file_data): # To start with log this is happening, later mark file as validation-failed current_app.logger.info( f"File still contains embedded fonts after remove_embedded_fonts for file name {filename}" ) else: current_app.logger.info( f"File no longer contains embedded fonts for file name {filename}" ) # during switchover, DWP and CYSP will still be sending the notify tag. Only add it if it's not already there if not is_notify_tag_present(file_data): file_data = add_notify_tag_to_letter(file_data) return file_data, recipient_address, redaction_failed_message
def test_convert_pdf_to_cmyk_preserves_black(client): data = BytesIO(rgb_black_pdf) assert does_pdf_contain_rgb(data) assert not does_pdf_contain_cmyk(data) result = convert_pdf_to_cmyk(data) doc = fitz.open(stream=result, filetype="pdf") first_image = doc.getPageImageList(pno=0)[0] image_object_number = first_image[0] pixmap = fitz.Pixmap(doc, image_object_number) assert 'CMYK' in str(pixmap.colorspace) assert pixmap.pixel(100, 100) == [0, 0, 0, 255] # [C,M,Y,K], where 'K' is black
def rewrite_pdf(file_data, *, page_count, allow_international_letters): file_data, recipient_address, redaction_failed_message = rewrite_address_block( file_data, page_count=page_count, allow_international_letters=allow_international_letters, ) if not does_pdf_contain_cmyk(file_data) or does_pdf_contain_rgb(file_data): file_data = convert_pdf_to_cmyk(file_data) if contains_unembedded_fonts(file_data): file_data = remove_embedded_fonts(file_data) # during switchover, DWP and CYSP will still be sending the notify tag. Only add it if it's not already there if not is_notify_tag_present(file_data): file_data = add_notify_tag_to_letter(file_data) return file_data, recipient_address, redaction_failed_message
def test_does_pdf_contain_cmyk(client, data, result): assert does_pdf_contain_cmyk(BytesIO(data)) == result
def test_convert_pdf_to_cmyk(client, data): result = convert_pdf_to_cmyk(BytesIO(data)) assert not does_pdf_contain_rgb(result) assert does_pdf_contain_cmyk(result)