[ADD] Report Fill PDF

This commit is contained in:
Enric Tobella
2017-12-05 09:36:37 +01:00
committed by Cyril VINH-TUNG
parent dc32616025
commit fa668adbfa
17 changed files with 404 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
from . import report_fill_pdf
from . import report_partner_pdf

View File

@@ -0,0 +1,65 @@
# Copyright 2017 Creu Blanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from io import BytesIO
import os
from contextlib import closing
import logging
import tempfile
from subprocess import Popen, PIPE
from odoo import api, models, tools
_logger = logging.getLogger(__name__)
try:
from fdfgen import forge_fdf
EXTERNAL_DEPENDENCY_BINARY_PDFTK = tools.find_in_path('pdftk')
except (ImportError, IOError) as err:
_logger.debug(err)
raise err
class ReportFillPDFAbstract(models.AbstractModel):
_name = 'report.report_fillpdf.abstract'
def fill_report(self, docids, data):
objs = self.env[self.env.context.get('active_model')].browse(docids)
return self.fill_pdf_form(
self.get_form(data, objs),
self.get_document_values(data, objs)), 'pdf'
@api.model
def get_original_document_path(self, data, objs):
raise NotImplementedError()
@api.model
def get_form(self, data, objs):
with open(self.get_original_document_path(data, objs), 'rb') as file:
result = file.read()
return result
@api.model
def get_document_values(self, data, objs):
return {}
@api.model
def fill_pdf_form(self, form, vals):
fdf = forge_fdf("", vals.items(), [], [], [])
document_fd, document_path = tempfile.mkstemp(
suffix='.pdf', prefix='')
with closing(os.fdopen(document_fd, 'wb')) as body_file:
body_file.write(form)
args = [
EXTERNAL_DEPENDENCY_BINARY_PDFTK,
document_path,
"fill_form", "-",
"output", "-",
"dont_ask",
"flatten"
]
p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate(fdf)
os.unlink(document_path)
if stderr.strip():
raise IOError(stderr)
return BytesIO(stdout).getvalue()

View File

@@ -0,0 +1,20 @@
# Copyright 2017 Creu Blanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import api, models
from odoo.modules import get_resource_path
class PartnerPDF(models.AbstractModel):
_name = 'report.report_fillpdf.partner_fillpdf'
_inherit = 'report.report_fillpdf.abstract'
@api.model
def get_original_document_path(self, data, objs):
return get_resource_path(
'report_fillpdf', 'static/src/pdf', 'partner_pdf.pdf')
@api.model
def get_document_values(self, data, objs):
objs.ensure_one()
return {'name': objs.name}