def test_PDFWriter(test_output_dir):
    pw = pdf_write.PDFWriter()

    font = pdf.PDFSimpleDict()
    font["Type"] = "Font"
    font["Subtype"] = "Type1"
    font["Name"] = "F1"
    font["BaseFont"] = "Helvetica"
    font["Encoding"] = "MaxRomanEncoding"
    font = pw.add_pdf_object(font.to_dict())
    resources = pdf.PDFSimpleDict()
    proc_set = pw.add_pdf_object(pdf_write.ProcedureSet().object())
    resources["ProcSet"] = proc_set
    resources["Font"] = pdf.PDFDictionary([(pdf.PDFName("F1"), font)])
    resources = resources.to_dict()
    data = b"BT\n/F1 24 Tf\n100 100 Td\n(Hello World) Tj\nET"
    contents = pdf.PDFStream(
        [(pdf.PDFName("Length"), pdf.PDFNumeric(len(data)))], data)
    contents = pw.add_pdf_object(contents)
    page = pdf_write.Page(pdf_write.Rectangle(0, 0, 612, 792), resources,
                          contents)

    pw.add_page(page)

    with open(os.path.join(test_output_dir, "text.pdf"), "wb") as f:
        f.write(bytes(pw))
Exemple #2
0
def test_JBIG2Images(test_output_dir, jb2zip):
    result = jb2.JBIG2Images(jb2zip, dpi=8).parts

    writer = pdf_write.PDFWriter()
    result.add_to_pdf_writer(writer)
    with open(os.path.join(test_output_dir, "jb2_test.pdf"), "wb") as f:
        f.write(bytes(writer))
Exemple #3
0
def test_FlateImage_rgb(rgb_image, test_output_dir):
    image = pdf_image.FlateImage(rgb_image)
    parts = image()

    writer = pdf_write.PDFWriter()
    parts.add_to_pdf_writer(writer)
    with open(os.path.join(test_output_dir, "flate_image_rgb.pdf"), "wb") as f:
        f.write(bytes(writer))
Exemple #4
0
def test_UncompressedImage_bw(blackwhite_image, test_output_dir):
    image = pdf_image.UncompressedImage(blackwhite_image)
    parts = image()

    writer = pdf_write.PDFWriter()
    parts.add_to_pdf_writer(writer)
    with open(os.path.join(test_output_dir, "uncompressed_image_bw.pdf"), "wb") as f:
        f.write(bytes(writer))
Exemple #5
0
def test_JPEGImage(grey_image, test_output_dir):
    image = pdf_image.JPEGImage(grey_image, quality=90)
    parts = image()

    writer = pdf_write.PDFWriter()
    parts.add_to_pdf_writer(writer)
    with open(os.path.join(test_output_dir, "jpeg_image_grey.pdf"), "wb") as f:
        f.write(bytes(writer))
Exemple #6
0
def test_TIFFImage(grey_image, test_output_dir):
    image = pdf_image.TIFFImage(grey_image)
    parts = image()

    writer = pdf_write.PDFWriter()
    parts.add_to_pdf_writer(writer)
    with open(os.path.join(test_output_dir, "tiff_image_grey.pdf"), "wb") as f:
        f.write(bytes(writer))
Exemple #7
0
def test_Flate_Multiple_Image4(test_output_dir, rgb_image, grey_image):
    image = pdf_image.FlateMultiImage(rgb_image)
    image.add_top_image(grey_image, (10, 20), (80, 150), (200, 255))
    parts = image()

    writer = pdf_write.PDFWriter()
    parts.add_to_pdf_writer(writer)
    with open(os.path.join(test_output_dir, "multi_flate_4.pdf"), "wb") as f:
        f.write(bytes(writer))
Exemple #8
0
def test_Flate_Multiple_Image1(test_output_dir, rgb_image, blackwhite_image):
    image = pdf_image.FlateMultiImage(blackwhite_image)
    image.add_top_image(rgb_image, (10, 20), (50, 50))
    parts = image()

    writer = pdf_write.PDFWriter()
    parts.add_to_pdf_writer(writer)
    with open(os.path.join(test_output_dir, "multi_flate_1.pdf"), "wb") as f:
        f.write(bytes(writer))
Exemple #9
0
def test_JPEGImageRaw(grey_image, test_output_dir):
    with io.BytesIO() as file:
        grey_image.save(file, format="JPEG", optimize=True, quality=95)
        buffer = file.getvalue()
    image = pdf_image.JPEGImageRaw(grey_image, buffer)
    parts = image()

    writer = pdf_write.PDFWriter()
    parts.add_to_pdf_writer(writer)
    with open(os.path.join(test_output_dir, "jpeg_raw_image_grey.pdf"), "wb") as f:
        f.write(bytes(writer))
Exemple #10
0
def test_UncompressedImage_combine(grey_image, palette_image, blackwhite_image, test_output_dir):
    writer = pdf_write.PDFWriter()

    parts = pdf_image.UncompressedImage(palette_image)()
    parts.add_to_pdf_writer(writer)
    parts = pdf_image.UncompressedImage(grey_image)()
    parts.add_to_pdf_writer(writer)
    parts = pdf_image.UncompressedImage(blackwhite_image)()
    parts.add_to_pdf_writer(writer)

    with open(os.path.join(test_output_dir, "uncompressed_three_up.pdf"), "wb") as f:
        f.write(bytes(writer))