odoo14里面给下载PDF附件加水印
依赖包:pip install reportlab
Odoo 中附件的下载会经过 ir.http 的 def binary_content() 方法获取附件内容等必要信息,
所以我们需要继承 ir.http 模型并重写 binary_content 方法,对 PDF 类型的附件添加水印,
在 [models.py](http://models.py) 中添加继承的代码:
import io
import base64
import logging
from reportlab.pdfbase import pdfmetrics, ttfonts
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
from PyPDF2 import PdfFileWriter, PdfFileReader from odoo import fields, models _logger = logging.getLogger(__name__) class IrHttp(models.AbstractModel):
_inherit = 'ir.http' def binary_content(self, *args, **kwargs):
status, headers, content = super(IrHttp, self).binary_content(*args, **kwargs)
content_type = dict(headers).get('Content-Type')
print ('------------content_type----------',content_type)
if content_type == 'application/pdf':
content = self.add_watermark(base64.b64decode(content))
content = base64.b64encode(content) return status, headers, content def _get_watermark(self):
"""
获取水印文本:公司名称+当前日期
:return:
"""
return f'{self.env.company.name} {fields.Date.context_today(self)}' def _generate_watermark(self):
"""
生成水印
:return:
"""
# 水印文件临时存储路径:
filename = f'E:\DEMO\watermark.pdf' #这是windows环境
# 水印文件临时存储路径
# filename = f'/tmp/watermark.pdf' #这是linux环境
watermark = self._get_watermark()
# 获取画布并修改原点坐标
c = canvas.Canvas(filename)
c.translate(1.5 * cm, -3 * cm) try:
font_name = 'SimSun'
# 从系统路径中引入中文字体(新宋)
pdfmetrics.registerFont(ttfonts.TTFont('SimSun', 'SimSun.ttf'))
except Exception as e:
# 默认字体,不支持中文
font_name = 'Helvetica'
_logger.error(f'Register Font Error: {e}') # 设置字体及大小,旋转 -20 度并设置颜色和透明度
c.setFont(font_name, 14)
c.rotate(20)
c.setFillColor('#27334C', 0.15)
# 平铺写入水印
print('watermark---------------', watermark)
for i in range(0, 30, 6):
for j in range(0, 35, 5):
c.drawString(i * cm, j * cm, watermark)
c.save()
return filename def add_watermark(self, content):
"""
添加水印
:param content:
:return:
"""
watermark = self._generate_watermark()
pdf_input = PdfFileReader(io.BytesIO(content), strict=False)
watermark = PdfFileReader(open(watermark, "rb"), strict=False)
pdf_output = PdfFileWriter()
page_count = pdf_input.getNumPages()
# 遍历要下载的 PDF 将它的每一页都和水印文件合并起来
for page_number in range(page_count):
input_page = pdf_input.getPage(page_number)
input_page.mergePage(watermark.getPage(0))
pdf_output.addPage(input_page)
stream = io.BytesIO()
pdf_output.write(stream)
data = stream.getvalue()
return data
最后效果

本文来自:https://ruterly.com/2020/12/27/Odoo-Add-Watermark-to-PDF
odoo14里面给下载PDF附件加水印的更多相关文章
- C#使用iTextSharp给PDF文件加水印
给PDF添加水印,可以用iTextSharp. 步骤1:下载iTextSharp 步骤2:在项目中添加引用itextsharp.dll 步骤3:在程序中使用iTextSharp.text.pdf us ...
- OAF 中下载使用XML Publisher下载PDF附件
OAF doesn't readily expose the Controller Servlet's HttpRequest and HttpResponse objects so you need ...
- springboot为导出的pdf和excel加水印
相信很多小伙伴们在做导出pdf或者excel文件时会被要求在文件上加上水印,本篇博客就来讲讲如何为pdf和excel加水印. 导出pdf加水印 其实在导出pdf时加上水印并不难,因为itext提供了添 ...
- Java 添加、下载、读取PDF附件信息(基于Spire.Cloud.SDK for Java)
Spire.Cloud.SDK for Java提供了PdfAttachmentsApi接口添加附件addAttachment().下载附件downloadAttachment().获取附件信息get ...
- 使用iText对pdf做权限的操作(不允许修改,不允许复制,不允许另存为),并且加水印等
添加水印,并且增加权限 @Test public void addWaterMark() throws Exception{ String srcFile="D:\\work\\pdf\\w ...
- php中使用head进行二进制流输出,让用户下载PDF等附件的方法
http://blog.csdn.net/jallin2001/article/details/6872951 在PHP的手册中,有如下的方法,可以让用户方便的下载pdf或者其他类似的附件形式,不过这 ...
- 给现有的word和pdf加水印
iTextSharp简单生成pdf和操作pdf添加水印 给word加水印,利用的是aspose.words
- 怎么编辑PDF,如何给PDF加水印
在使用PDF文件的时候,往往会用到PDF编辑器来修改,那么,在使用PDF编辑器修改文件的时候,想要在文件中添加水印,这该怎么操作呢,不会的小伙伴可以看看下面的文章了哦,说不定就会了. 1.打开运行PD ...
- java 实现 PDF 加水印功能
使用java代码实现给PDF加水印的功能 首先导入所需要的依赖 <dependency> <groupId>com.itextpdf</groupId> <a ...
随机推荐
- 使用有道云笔记还是github写笔记的优缺点对比
有道云笔记的优点 在手机上编辑笔记,有道云的体验更好,建议:新建普通类型笔记而不是markdown笔记,因为md在手机上的编辑体验并不好 插入图片方便无需考虑图床 可以把笔记分享到社交平台(QQ,微信 ...
- 【NX二次开发】Block UI 反向
属性说明 属性 类型 描述 常规 BlockID String 控件ID Enable Logical 是否可操作 Group ...
- js02
一.<thead></thead>,<tbody></tbody>:为了使表头和表格内容分开设置样式 1.tbody里边有一个rows.length,获 ...
- 2020想进大厂你不得不了解的MySQL意外事件的查询技巧
导读:数据库是导致应用系统运行缓慢的常见原因.面对数据库引性能问题,很多开发者或者DBA却束手无策.本文作者经过多年的实际经验,整理了一些材料,将Linux环境下MySQL性能突发事件问题排查技巧分享 ...
- windows 安装Git详解
windows 安装Git详解 一.Git简介 Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理. Git 是 Linus Torvalds 为了帮助管理 Lin ...
- excel匹配函数vlookup和lookup
1.vlookup(查找的条件,查找的区域,满足查找条件后需要返回的值在选中的查找区域的第几列,精确匹配还是近似匹配(精确匹配为0或False表示,反之为1或True)) =VLOOKUP(J2,$G ...
- Kubernetes之deployment
Kubernetes实现了零停机的升级过程.升级操作可以通过使用ReplicationController或者ReplicaSet实现,但是Kubernetes提供了另一种基于ReplicaSet的资 ...
- Windows10上开启WSL2(Windows Subsystem for Linux 2)及Docker Desktop For Windows
什么是WSL2 WSL2(Windows Subsystem for Linux 2)是适用于Linux的Windows子系统体系结构的一个新版本,它支持适用于Linux的Windows子系统在Win ...
- 14、CentOS7安装过程中,磁盘大于2T的报错处理
问题描述 服务器磁盘单盘空间大于2TB,在安装CentOS7时出现下图报错: Boot failure.Reboot and Select proper Boot device... 问题原因: 安装 ...
- 9.4、安装zabbix(1)
1.什么是zabbix: zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案: zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以 ...