โดย MBMoo » 16/08/2022 2:55 pm
โค้ด: เลือกทั้งหมด
def bo(self):
out , dir, file_len = self.pdf_to_image
image_base_64 = self.image_to_base64(out , dir, file_len)
return image_base_64
def pdf_to_image(self):
attach_fname = 'bo.pdf'
attach_fname_jpj = attach_fname.replace(".pdf", ".jpeg")
dir = self.create_dir()
jpeg_report_path = os.path.join(dir, attach_fname_jpj)
fname_new = ' "' + jpeg_report_path + '"'
if company:
density = company.md_btn_sale_line_tmp_image
pdf_report_path = os.path.join(dir, attach_fname)
with open(pdf_report_path, 'wb') as theFile:
theFile.write(base64.b64decode(attach_datas))
process = subprocess.Popen(r'Magick convert -verbose -density '+ pdf_report_path + fname_new,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
file_len = len([name for name in os.listdir(dir) if os.path.isfile(os.path.join(dir, name))]) # นับไฟล์ที่อยู่ในโฟล์เดอร์ที่เราสร้างขึ้น
return out , dir, file_len - 1 # ที่เอามาลบ 1 เพราะมี pdf อยู่ด้วย ถ้าแยกเก็บคนละโฟล์เดอร์ไม่ต้องลบก็ได้
def create_dir(self):
dt = datetime.now()
ts = str(datetime.timestamp(dt))
directory = "md_pdf_to_image" + ts
parent_dir = os.path.join(r"D:\path", self._cr.dbname)
path = os.path.join(parent_dir, directory)
os.mkdir(path)
return path
def image_to_base64(self, out, dir, file_len):
image_base_64 = []
store = False
for no in range(file_len):
new = "-%s.jpeg" % no
name_jpg = attach_fname.replace(".pdf", new)
jpeg_report_path = os.path.join(dir, name_jpg)
with open(jpeg_report_path, "rb") as image_file:
image_base_64.append({
'base64': base64.b64encode(image_file.read()),
})
os.rmdir(dir)
return image_base_64
ค่าใน image_base_64 ที่ได้
โค้ด: เลือกทั้งหมด
[{'base64': base64_str}, {'base64': base64_str}, {'base64': base64_str}]
[code]
def bo(self):
out , dir, file_len = self.pdf_to_image
image_base_64 = self.image_to_base64(out , dir, file_len)
return image_base_64
def pdf_to_image(self):
attach_fname = 'bo.pdf'
attach_fname_jpj = attach_fname.replace(".pdf", ".jpeg")
dir = self.create_dir()
jpeg_report_path = os.path.join(dir, attach_fname_jpj)
fname_new = ' "' + jpeg_report_path + '"'
if company:
density = company.md_btn_sale_line_tmp_image
pdf_report_path = os.path.join(dir, attach_fname)
with open(pdf_report_path, 'wb') as theFile:
theFile.write(base64.b64decode(attach_datas))
process = subprocess.Popen(r'Magick convert -verbose -density '+ pdf_report_path + fname_new,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
file_len = len([name for name in os.listdir(dir) if os.path.isfile(os.path.join(dir, name))]) # นับไฟล์ที่อยู่ในโฟล์เดอร์ที่เราสร้างขึ้น
return out , dir, file_len - 1 # ที่เอามาลบ 1 เพราะมี pdf อยู่ด้วย ถ้าแยกเก็บคนละโฟล์เดอร์ไม่ต้องลบก็ได้
def create_dir(self):
dt = datetime.now()
ts = str(datetime.timestamp(dt))
directory = "md_pdf_to_image" + ts
parent_dir = os.path.join(r"D:\path", self._cr.dbname)
path = os.path.join(parent_dir, directory)
os.mkdir(path)
return path
def image_to_base64(self, out, dir, file_len):
image_base_64 = []
store = False
for no in range(file_len):
new = "-%s.jpeg" % no
name_jpg = attach_fname.replace(".pdf", new)
jpeg_report_path = os.path.join(dir, name_jpg)
with open(jpeg_report_path, "rb") as image_file:
image_base_64.append({
'base64': base64.b64encode(image_file.read()),
})
os.rmdir(dir)
return image_base_64
[/code]
ค่าใน image_base_64 ที่ได้
[code]
[{'base64': base64_str}, {'base64': base64_str}, {'base64': base64_str}]
[/code]