<commit_before>import os
import base64
import tempfile
import io
import zlib

from PyPDF2 import PdfFileReader, PdfFileWriter
from PyPDF2.utils import PdfReadError

from reportlab.pdfgen import canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.utils import ImageReader
from reportlab.pdfbase import pdfmetrics

from wand.image import Image
from wand.drawing import Drawing
from wand.color import Color

from .document import (
    PDF_FILETYPES, decrypt_pdf_in_place, rewrite_pdf_in_place
)


def can_redact_file(filetype, name=None):
    return filetype.lower() in PDF_FILETYPES or (
        name is not None and name.lower().endswith('.pdf')
    )


def redact_file(pdf_file, instructions):
    dpi = 300
    load_invisible_font()
    output = PdfFileWriter()
    try:
        pdf_reader = PdfFileReader(pdf_file, strict=False)
    except PdfReadError:
        pdf_file_name = rewrite_pdf_in_place(pdf_file.name)
        if pdf_file_name is None:<commit_msg>Catch errors not caught by PyPDF and try fixing<commit_after>import os
import base64
import tempfile
import io
import zlib

from PyPDF2 import PdfFileReader, PdfFileWriter
from PyPDF2.utils import PdfReadError

from reportlab.pdfgen import canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.utils import ImageReader
from reportlab.pdfbase import pdfmetrics

from wand.image import Image
from wand.drawing import Drawing
from wand.color import Color

from .document import (
    PDF_FILETYPES, decrypt_pdf_in_place, rewrite_pdf_in_place
)


def can_redact_file(filetype, name=None):
    return filetype.lower() in PDF_FILETYPES or (
        name is not None and name.lower().endswith('.pdf')
    )


def redact_file(pdf_file, instructions):
    dpi = 300
    load_invisible_font()
    output = PdfFileWriter()
    try:
        pdf_reader = PdfFileReader(pdf_file, strict=False)
    except (PdfReadError, ValueError):
        pdf_file_name = rewrite_pdf_in_place(pdf_file.name)
        if pdf_file_name is None:
{'fraction_non_alphanumeric': 0.06119402985074627, 'fraction_numerical': 0.006965174129353234, 'mean_word_length': 4.377005347593583, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '3595807', 'n_tokens_mistral': 652, 'n_tokens_neox': 636, 'n_words': 182}