示例#1
0
def main():
    filename = pathlib.Path(PDF_FILE_IN)
    password = '******'

    with Pdf.open(filename) as pdf:
        filename_encrypted = filename.parents[
            0] / f"{filename.stem}_encrypted.pdf"
        pdf.save(filename_encrypted,
                 encryption=Encryption(user=password, owner=password))
示例#2
0
def compile_all(exam, subtitle, out, do_twice, email, exam_type, semester,
                deadline):
    """
    Compile individualized PDFs for the specified exam.
    Exam must have been deployed first.
    """
    if not out:
        out = "out/latex/" + exam

    pathlib.Path(out).mkdir(parents=True, exist_ok=True)

    exam_data = get_exam(exam=exam)
    password = exam_data.pop("secret")[:-1]
    print(password)
    exam_str = json.dumps(exam_data)

    roster = get_roster(exam=exam)

    if email:
        roster = [line_info for line_info in roster if line_info[0] == email]
        if len(roster) == 0:
            if deadline:
                roster = [(email, deadline)]
            else:
                raise ValueError("Email does not exist in the roster!")

    for email, deadline in roster:
        if not deadline:
            continue
        exam_data = json.loads(exam_str)
        scramble(email, exam_data)
        deadline_utc = datetime.utcfromtimestamp(int(deadline))
        deadline_pst = pytz.utc.localize(deadline_utc).astimezone(
            pytz.timezone("America/Los_Angeles"))
        deadline_string = deadline_pst.strftime("%I:%M%p")

        with render_latex(
                exam_data,
            {
                "emailaddress": sanitize_email(email),
                "deadline": deadline_string,
                "coursecode": prettify(exam.split("-")[0]),
                "description": subtitle,
                "examtype": exam_type,
                "semester": semester,
            },
                do_twice=do_twice,
        ) as pdf:
            pdf = Pdf.open(BytesIO(pdf))
            pdf.save(
                os.path.join(
                    out, "exam_" + email.replace("@", "_").replace(".", "_") +
                    ".pdf"),
                encryption=Encryption(owner=password, user=password),
            )
            pdf.close()
示例#3
0
def protegendo_pdf_senha():
    with Pdf.open('PDF_Exemplo4.pdf') as pdf:
        restricoes = Permissions(extract=False,
                                 print_highres=False,
                                 modify_form=False)
        pdf.save('PDF_protegido_com_senha.pdf',
                 encryption=Encryption(user='******',
                                       allow=restricoes,
                                       owner='senha'))
        pdf.close()
示例#4
0
def encryptFile(filename, password):
    try:
        filename_not_encrypted = filename[:-4]+'.pdf'
        filename_encrypted = filename.strip('.pdf')+'_encrypted.pdf'
        pdf = Pdf.open(filename_not_encrypted)
        pdf.save(filename_encrypted, encryption=Encryption(
            owner="base_password_to_open_books", user=password, R=4))
        pdf.close()
        return True
    except Exception as e:
        print("Already encrypted file")
        return False
示例#5
0
if __name__ == '__main__':
    # images path(folder)
    image_path = ''
    # pdf file name without suffix
    file_name = ''
    images = [
        os.path.join(image_path, filename)
        for filename in os.listdir(image_path)
    ]
    images = natsorted(images, key=take_filename)

    image_list = []

    for image in images:
        print(image)
        image_list.append(Image.open(image).convert('RGB'))

    if not os.path.exists('tmp'):
        os.makedirs('tmp')

    image_list[0].save(f'tmp/{file_name}.pdf',
                       save_all=True,
                       append_images=image_list[1:])
    with Pdf.open(f'tmp/{file_name}.pdf') as pdf:
        # owner: owner password
        # user: view password
        pdf.save(f'{file_name}.pdf',
                 encryption=Encryption(owner='owner-pwd', user='******'))
    os.remove(f'tmp/{file_name}.pdf')