summaryrefslogtreecommitdiff
path: root/libmat2/pdf.py
diff options
context:
space:
mode:
authorAntonio Eugenio Burriel2021-07-25 14:10:36 +0200
committerjvoisin2021-07-25 14:12:57 +0200
commit3b094ae449afbb2c375454c1ee76b76aa98648d4 (patch)
treeaf92542a2d3066997a5cee1f7444247b2fa375eb /libmat2/pdf.py
parent0b094b594bd1db017ed3d063a10714f6b2a7b9f3 (diff)
Fix pdf issues on printers
pyCairo by default renders the PDF surfaces with a resolution of 72 dpi which is so low that the bitmap gets blurred compared to original. Since pyCairo 1.12.0, a new method set_device_scale(x_scale, y_scale) is added, which allows changing the canvas resolution.
Diffstat (limited to '')
-rw-r--r--libmat2/pdf.py13
1 files changed, 9 insertions, 4 deletions
diff --git a/libmat2/pdf.py b/libmat2/pdf.py
index e258512..fc5f7dc 100644
--- a/libmat2/pdf.py
+++ b/libmat2/pdf.py
@@ -32,7 +32,7 @@ class PDFParser(abstract.AbstractParser):
32 def __init__(self, filename): 32 def __init__(self, filename):
33 super().__init__(filename) 33 super().__init__(filename)
34 self.uri = 'file://' + os.path.abspath(self.filename) 34 self.uri = 'file://' + os.path.abspath(self.filename)
35 self.__scale = 2 # how much precision do we want for the render 35 self.__scale = 200 / 72.0 # how much precision do we want for the render
36 try: # Check now that the file is valid, to avoid surprises later 36 try: # Check now that the file is valid, to avoid surprises later
37 Poppler.Document.new_from_file(self.uri, None) 37 Poppler.Document.new_from_file(self.uri, None)
38 except GLib.GError: # Invalid PDF 38 except GLib.GError: # Invalid PDF
@@ -90,8 +90,8 @@ class PDFParser(abstract.AbstractParser):
90 page_width, page_height = page.get_size() 90 page_width, page_height = page.get_size()
91 logging.info("Rendering page %d/%d", pagenum + 1, pages_count) 91 logging.info("Rendering page %d/%d", pagenum + 1, pages_count)
92 92
93 width = int(page_width) * self.__scale 93 width = int(page_width * self.__scale)
94 height = int(page_height) * self.__scale 94 height = int(page_height * self.__scale)
95 img_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) 95 img_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
96 img_context = cairo.Context(img_surface) 96 img_context = cairo.Context(img_surface)
97 97
@@ -105,7 +105,12 @@ class PDFParser(abstract.AbstractParser):
105 buf.seek(0) 105 buf.seek(0)
106 106
107 img = cairo.ImageSurface.create_from_png(buf) 107 img = cairo.ImageSurface.create_from_png(buf)
108 pdf_surface.set_size(page_width*self.__scale, page_height*self.__scale) 108 if cairo.version_info < (1, 12, 0):
109 pdf_surface.set_size(width, height)
110 else:
111 print("lol")
112 pdf_surface.set_size(page_width, page_height)
113 pdf_surface.set_device_scale(1 / self.__scale, 1 / self.__scale)
109 pdf_context.set_source_surface(img, 0, 0) 114 pdf_context.set_source_surface(img, 0, 0)
110 pdf_context.paint() 115 pdf_context.paint()
111 pdf_context.show_page() # draw pdf_context on pdf_surface 116 pdf_context.show_page() # draw pdf_context on pdf_surface