Ejemplo n.º 1
0
def create_pdf(source_image_file, ta_pages, config, output_filename):
    """透明なテキストと画像入りのPDFを作成するメソッド"""

    print("processing pdf: {0}".format(output_filename))

    is_normalized = False

    # PDFまたは画像をページ分割
    if re.search(r'\.pdf$', source_image_file):
        images = convert_pdf_to_img(source_image_file,
                                    dpi=config.image_resolution)
        is_normalized = True

    elif re.search(r'\.tiff$', source_image_file):
        images = divide_tiff_image(source_image_file)
    else:
        print("Non-support file type. Existed!", file=sys.stderr)
        sys.exit(-1)

    newPdfPage = canvas.Canvas(output_filename)
    newPdfPage.setPageSize(A4)

    newPdfPage.saveState()  # 念の為

    newPdfPage.setAuthor(config.author)
    newPdfPage.setTitle(config.title)
    newPdfPage.setSubject(config.subject)

    # 日本語用のフォントの登録(language packに含まれるもの)
    pdfmetrics.registerFont(UnicodeCIDFont('HeiseiMin-W3'))
    pdfmetrics.registerFont(UnicodeCIDFont('HeiseiKakuGo-W5'))

    # tiff file, PDF
    for i, image in enumerate(images):
        print(f"start page: {i}")

        print("image size: {}".format(image.size))
        image_width, image_height = image.size

        ratio = image_width / image_height

        landscape_mode = False
        page_size = {}

        if ratio > 1.0:
            landscape_mode = True
            newPdfPage.setPageSize(landscape(A4))
            page_size['width'], page_size['height'] = landscape(A4)
        else:
            newPdfPage.setPageSize(A4)
            page_size['width'], page_size['height'] = A4

        offset_y = 2.0
        offset_x = -1.0
        image_offset_x = 0
        image_offset_y = 0

        print("page size: {0}, {1}".format(page_size['width'],
                                           page_size['height']))
        with tempfile.NamedTemporaryFile(mode='w+b', suffix='.jpg') as fp:

            image.save(fp.name, format='jpeg', quality=config.jpeg_quality)

            if config.image_embeded:
                newPdfPage.drawImage(fp.name,
                                     0 + image_offset_x,
                                     0 + image_offset_y,
                                     width=page_size['width'],
                                     height=page_size['height'],
                                     preserveAspectRatio=True,
                                     anchor='s')

        newPdfPage.setFont(config.default_fontname, 10)

        # 文字色と透明度の設定
        newPdfPage.setFillColor(red, alpha=0.0)

        page = ta_pages[i]

        scale = 1.0
        if landscape_mode:
            scale = min(page_size['height'] / image_height,
                        page_size['width'] / image_width)

        else:
            scale = min(page_size['height'] / image_height,
                        page_size['width'] / image_width)

        if is_normalized:
            scale = 1.0
            for block in page.blocks:
                for p in block.paragraphs:
                    for word in p.words:

                        text = ''.join([t.text for t in word.symbols])
                        anchor_y = int(page_size['height'] * (1.0 - float(
                            word.bounding_box.normalized_vertices[3].y))
                                       ) + offset_y

                        anchor_x = int(page_size['width'] * float(
                            word.bounding_box.normalized_vertices[3].x)
                                       ) + offset_x

                        text_height = int(
                            page_size['height'] *
                            (word.bounding_box.normalized_vertices[3].y -
                             word.bounding_box.normalized_vertices[0].y))

                        font_size = text_height

                        newPdfPage.setFont(config.default_fontname, font_size)

                        newPdfPage.drawString(anchor_x, anchor_y, text)

            newPdfPage.showPage()
        else:

            for block in page.blocks:
                for p in block.paragraphs:
                    for w in p.words:

                        for node in w.symbols:
                            #print(node)

                            anchor_y = image_height - int(
                                node.bounding_box.vertices[3].y)
                            anchor_x = int(node.bounding_box.vertices[3].x)

                            text_height = int(
                                node.bounding_box.vertices[3].y) - int(
                                    node.bounding_box.vertices[0].y)

                            font_size = config.font_adjustment * math.floor(
                                text_height / (config.image_resolution / 72))

                            newPdfPage.setFont(config.default_fontname,
                                               font_size)

                            newPdfPage.drawString(scale * anchor_x,
                                                  scale * anchor_y, node.text)

            newPdfPage.showPage()

    newPdfPage.save()
Ejemplo n.º 2
0
    def phraseSheet(self, inputFileName, outputFileName):
        # Create a new canvas object to draw into
        canv = canvas.Canvas(outputFileName, pagesize=A4, bottomup=0)

        # Register STSong as a font. We'll use it for chinese writing
        pdfmetrics.registerFont(UnicodeCIDFont('STSong-Light'))
        pdfmetrics.registerFont(
            TTFont('Merriweather-Bold', "Merriweather-Bold.ttf"))

        # Draw header information. Name and worksheet name
        self.drawOpening(canv, "汉字练习纸", "Chinese Phrase Writing Sheet")

        # Where (y-coordinate) to start writing the rows and
        # by how much each line should be spaced for among
        # themselves (vertically).
        vertPosition = self.firstPageVertPosStart

        # Counter for the index of current page and number of rows
        # in current page.
        pageCounter = 1

        # Number of phrase rows/boxes to be drawn on the current page.
        # This is different for first page and inner pages.
        currMaxRowPerPage = self.MaxRowsFirstPage

        # Read all content of the file into a list
        fp = open(inputFileName)
        rows = tuple(fp)

        # Draw first page's footer
        self.drawFooter(canv, pageCounter,
                        len(rows) / 3)

        # Iterate over all lines of the input file.
        # lineIdx always should point to a valid verbatim phrase
        # logic inside the loop access the mandarin phrase
        for lineIdx in range(0, len(rows), 3):
            verbText = rows[lineIdx][:-1]
            mandText = rows[lineIdx + 1][:-1]

            # Draw one entire phrase. One at a time.
            for ind in range(0, currMaxRowPerPage):
                # Each column gets progressively opaque, the little
                # math below is for linearly increasing the chracter
                # opacity. Furthermore, opacity cannot be lower than
                # zero, therefore we take care of that too.
                alpha = 1.0 - ind / 10
                canv.setFillAlpha(alpha if (alpha >= 0) else 0)

                # Draw one single phrase
                self.drawPhraseBox(canv, vertPosition, verbText, mandText)

                # Increment in horizontal offset for the next character
                # box
                vertPosition += self.vertIncrement

            # If we did not create pages for all phrases we add a new page
            if ((lineIdx + 3) != len(rows)):
                # Make the just filled page to show up and start a new one
                canv.showPage()

                pageCounter += 1
                currMaxRowPerPage = self.MaxRowsInnerPages
                vertPosition = self.innerPagesVertPosStart

                # Draw a footer on the recently created page
                self.drawFooter(canv, pageCounter,
                                len(rows) / 3)

        # Save PDF to file
        canv.save()
Ejemplo n.º 3
0
    def characterSheet(self, inputFileName, outputFileName):
        # Create a new canvas object to draw into
        canv = canvas.Canvas(outputFileName, pagesize=A4, bottomup=0)

        # Register STSong as a font. We'll use it for chinese writing
        pdfmetrics.registerFont(UnicodeCIDFont('STSong-Light'))
        pdfmetrics.registerFont(
            TTFont('Merriweather-Bold', "Merriweather-Bold.ttf"))

        # Draw header information. Name and worksheet name
        self.drawOpening(canv, "汉字练习纸", "Chinese Character Writing Sheet")

        # Where (y-coordinate) to start writing the rows and
        # by how much each line should be spaced for among
        # themselves (vertically).
        vertPosition = self.firstPageVertPosStart

        # Counter for the index of current page and number of rows
        # in current page.
        pageCounter = 1
        rowCounter = 0

        # Read all content of the file into a list
        rows = tuple(open(inputFileName))

        # Draw first page's footer
        self.drawFooter(canv, pageCounter, self.numPages(len(rows)))

        # Iterate over all lines from input file
        for line in rows:
            # Extract the pinyin and the character apart
            parts = line.split()

            # Where (x-coordinate) to start writing the rows and
            # by how much each line should be spaced for among
            # themselves (horizontally).
            horPosition = self.horPositionStart

            # Draw one entire row of characters. One at a time.
            for ind in range(0, 11):
                # Each column gets progressively opaque, the little
                # math below is for linearly increasing the chracter
                # opacity. Furthermore, opacity cannot be lower than
                # zero, therefore we take care of that too.
                alpha = 1.0 - ind / 10
                canv.setFillAlpha(alpha if (alpha >= 0) else 0)

                # Draw one single characters
                self.drawCharacterBox(canv, (horPosition, vertPosition),
                                      parts[0], parts[1])

                # Increment in horizontal offset for the next character
                # box
                horPosition += self.horIncrement

            # Increment in horizontal offset for the next character
            # box
            vertPosition += self.vertIncrement

            # Account for the just written character row
            rowCounter += 1

            # If the bottom of the page was reached, i.e., the maximum number of
            # rows per page, we create a new page to draw into and reset vertical
            # offsets.
            if (rowCounter % self.currMaxRowPerPage == 0):
                # Make the just filled page to show up and start a new one
                canv.showPage()

                pageCounter += 1
                rowCounter = 0
                currMaxRowPerPage = self.MaxRowsInnerPages
                vertPosition = self.innerPagesVertPosStart

                # Draw a footer on the recently created page
                self.drawFooter(canv, pageCounter, self.numPages(len(rows)))

        # Save PDF to file
        canv.save()
Ejemplo n.º 4
0
def set_asian_fonts(fontname):
    font_dict = copy(reportlab.pdfbase._cidfontdata.defaultUnicodeEncodings)
    fonts = font_dict.keys()
    if fontname in fonts:
        pdfmetrics.registerFont(UnicodeCIDFont(fontname))
Ejemplo n.º 5
0
def set_up(fname='address.pdf'):
    pdfmetrics.registerFont(UnicodeCIDFont('HeiseiMin-W3', isVertical=True))
    pdfmetrics.registerFont(UnicodeCIDFont('HeiseiKakuGo-W5'))
    pdf = canvas.Canvas(fname)
    pdf.setPageSize((10*cm, 14.8*cm))
    return pdf
Ejemplo n.º 6
0
# クラスのインポート
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
import reportlab.lib.units as unit
import reportlab.lib.pagesizes as pagesizes
# フォントの登録
pdfmetrics.registerFont(UnicodeCIDFont("HeiseiKakuGo-W5"))
# PDFを作る
pdf = canvas.Canvas("example.pdf", pagesize=pagesizes.A4)
moji = "あ"
# フォントの大きさは用紙横幅と同じ210mmとする
pdf.setFont("HeiseiKakuGo-W5", 210 * unit.mm)
# 高さ
h = (297 - 210) / 2 * unit.mm
pdf.drawString(0 * unit.mm, h, moji)
pdf.save()