|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Contains v2 user API for PyPDFForm.""" |
| 3 | + |
| 4 | +from typing import BinaryIO, Dict, Union |
| 5 | + |
| 6 | +from ..core.filler import Filler as FillerCore |
| 7 | +from ..core.font import Font as FontCore |
| 8 | +from ..core.image import Image as ImageCore |
| 9 | +from ..core.template import Template as TemplateCore |
| 10 | +from ..core.utils import Utils as UtilsCore |
| 11 | +from ..core.watermark import Watermark as WatermarkCore |
| 12 | +from .adapter import FileAdapter |
| 13 | +from .constants import Text as TextConstants |
| 14 | +from .element import Element as ElementMiddleware |
| 15 | +from .element import ElementType |
| 16 | +from .template import Template as TemplateMiddleware |
| 17 | + |
| 18 | + |
| 19 | +class WrapperV2: |
| 20 | + """A class to represent a PDF form.""" |
| 21 | + |
| 22 | + def __init__( |
| 23 | + self, |
| 24 | + template: Union[bytes, str, BinaryIO] = b"", |
| 25 | + **kwargs, |
| 26 | + ) -> None: |
| 27 | + """Constructs all attributes for the PyPDFForm object.""" |
| 28 | + |
| 29 | + self.stream = FileAdapter().fp_or_f_obj_or_stream_to_stream(template) |
| 30 | + self.elements = ( |
| 31 | + TemplateMiddleware().build_elements_v2(self.stream) if self.stream else {} |
| 32 | + ) |
| 33 | + |
| 34 | + for each in self.elements.values(): |
| 35 | + if each.type == ElementType.text: |
| 36 | + each.font = kwargs.get("global_font", TextConstants().global_font) |
| 37 | + each.font_size = kwargs.get( |
| 38 | + "global_font_size", TextConstants().global_font_size |
| 39 | + ) |
| 40 | + each.font_color = kwargs.get( |
| 41 | + "global_font_color", TextConstants().global_font_color |
| 42 | + ) |
| 43 | + each.text_x_offset = kwargs.get( |
| 44 | + "global_text_x_offset", TextConstants().global_text_x_offset |
| 45 | + ) |
| 46 | + each.text_y_offset = kwargs.get( |
| 47 | + "global_text_y_offset", TextConstants().global_text_y_offset |
| 48 | + ) |
| 49 | + each.text_wrap_length = kwargs.get( |
| 50 | + "global_text_wrap_length", TextConstants().global_text_wrap_length |
| 51 | + ) |
| 52 | + |
| 53 | + def read(self) -> bytes: |
| 54 | + """Reads the file stream of a PDF form.""" |
| 55 | + |
| 56 | + return self.stream |
| 57 | + |
| 58 | + def __add__(self, other: "WrapperV2") -> "WrapperV2": |
| 59 | + """Overloaded addition operator to perform merging PDFs.""" |
| 60 | + |
| 61 | + if not self.stream: |
| 62 | + return other |
| 63 | + |
| 64 | + if not other.stream: |
| 65 | + return self |
| 66 | + |
| 67 | + new_obj = self.__class__() |
| 68 | + new_obj.stream = UtilsCore().merge_two_pdfs(self.stream, other.stream) |
| 69 | + |
| 70 | + return new_obj |
| 71 | + |
| 72 | + def fill( |
| 73 | + self, |
| 74 | + data: Dict[str, Union[str, bool, int]], |
| 75 | + ) -> "WrapperV2": |
| 76 | + """Fill a PDF form.""" |
| 77 | + |
| 78 | + for key, value in data.items(): |
| 79 | + if key in self.elements: |
| 80 | + self.elements[key].value = value |
| 81 | + |
| 82 | + if self.read(): |
| 83 | + self.elements = TemplateMiddleware().set_character_x_paddings( |
| 84 | + self.stream, self.elements |
| 85 | + ) |
| 86 | + |
| 87 | + self.stream = TemplateCore().remove_all_elements( |
| 88 | + FillerCore().fill_v2(self.stream, self.elements) |
| 89 | + ) |
| 90 | + |
| 91 | + return self |
| 92 | + |
| 93 | + def draw_text( |
| 94 | + self, |
| 95 | + text: str, |
| 96 | + page_number: int, |
| 97 | + x: Union[float, int], |
| 98 | + y: Union[float, int], |
| 99 | + **kwargs, |
| 100 | + ) -> "WrapperV2": |
| 101 | + """Draws a text on a PDF form.""" |
| 102 | + |
| 103 | + new_element = ElementMiddleware("new", ElementType.text) |
| 104 | + new_element.value = text |
| 105 | + new_element.font = kwargs.get("font", TextConstants().global_font) |
| 106 | + new_element.font_size = kwargs.get( |
| 107 | + "font_size", TextConstants().global_font_size |
| 108 | + ) |
| 109 | + new_element.font_color = kwargs.get( |
| 110 | + "font_color", TextConstants().global_font_color |
| 111 | + ) |
| 112 | + new_element.text_x_offset = kwargs.get( |
| 113 | + "text_x_offset", TextConstants().global_text_x_offset |
| 114 | + ) |
| 115 | + new_element.text_y_offset = kwargs.get( |
| 116 | + "text_y_offset", TextConstants().global_text_y_offset |
| 117 | + ) |
| 118 | + new_element.text_wrap_length = kwargs.get( |
| 119 | + "text_wrap_length", TextConstants().global_text_wrap_length |
| 120 | + ) |
| 121 | + |
| 122 | + watermarks = WatermarkCore().create_watermarks_and_draw( |
| 123 | + self.stream, |
| 124 | + page_number, |
| 125 | + "text", |
| 126 | + [ |
| 127 | + [ |
| 128 | + new_element, |
| 129 | + x, |
| 130 | + y, |
| 131 | + ] |
| 132 | + ], |
| 133 | + ) |
| 134 | + |
| 135 | + self.stream = WatermarkCore().merge_watermarks_with_pdf(self.stream, watermarks) |
| 136 | + |
| 137 | + return self |
| 138 | + |
| 139 | + def draw_image( |
| 140 | + self, |
| 141 | + image: Union[bytes, str, BinaryIO], |
| 142 | + page_number: int, |
| 143 | + x: Union[float, int], |
| 144 | + y: Union[float, int], |
| 145 | + width: Union[float, int], |
| 146 | + height: Union[float, int], |
| 147 | + rotation: Union[float, int] = 0, |
| 148 | + ) -> "WrapperV2": |
| 149 | + """Draws an image on a PDF form.""" |
| 150 | + |
| 151 | + image = FileAdapter().fp_or_f_obj_or_stream_to_stream(image) |
| 152 | + image = ImageCore().any_image_to_jpg(image) |
| 153 | + image = ImageCore().rotate_image(image, rotation) |
| 154 | + watermarks = WatermarkCore().create_watermarks_and_draw( |
| 155 | + self.stream, page_number, "image", [[image, x, y, width, height]] |
| 156 | + ) |
| 157 | + |
| 158 | + self.stream = WatermarkCore().merge_watermarks_with_pdf(self.stream, watermarks) |
| 159 | + |
| 160 | + return self |
| 161 | + |
| 162 | + def generate_schema(self) -> dict: |
| 163 | + """Generates a json schema for the PDF form template.""" |
| 164 | + |
| 165 | + result = { |
| 166 | + "type": "object", |
| 167 | + "properties": { |
| 168 | + key: value.schema_definition for key, value in self.elements.items() |
| 169 | + }, |
| 170 | + } |
| 171 | + |
| 172 | + return result |
| 173 | + |
| 174 | + @classmethod |
| 175 | + def register_font( |
| 176 | + cls, font_name: str, ttf_file: Union[bytes, str, BinaryIO] |
| 177 | + ) -> bool: |
| 178 | + """Registers a font from a ttf file.""" |
| 179 | + |
| 180 | + ttf_file = FileAdapter().fp_or_f_obj_or_stream_to_stream(ttf_file) |
| 181 | + |
| 182 | + return FontCore().register_font(font_name, ttf_file) |
0 commit comments