扫描二维码实现文档预览的基本原理是把Tomcat作为文件服务器,将文档存入Tomcat文件服务器中,然后再把链接作为内容绘制二维码。此方法需要手机能够访问到服务器。注:服务器为Linux环境。
1. 配置环境
1.1 安装jdk
搭建Tomcat文件服务器需要java环境,所以首先安装jdk,链接:https://pan.baidu.com/s/1s77aE1h2T32hqybx8rm9ng?pwd=6k58 提取码:6k58 。
- 将上传的jdk压缩包解压,进入压缩包所在目录,执行如下命令将jdk解压,并通过-C参数指定解压文件存放目录为 /opt/software:
tar -zxvf jdk-8u171-linux-x64.tar.gz -C /opt/software
- 配置环境变量,使用vi命令修改/etc/profile文件,在文件末尾加入如下配置:
JAVA_HOME=/opt/software/jdk1.8.0_171
PATH=$JAVA_HOME/bin:$PATH
- 重新加载profile文件,为了使更改的配置立即生效,需要重新加载profile文件,执行命令:
source /etc/profile
- 检查jdk安装是否成功
1.2 搭建Tomcat文件服务器
tomcat下载链接:https://pan.baidu.com/s/1M7igfAsqsZxgbx4nNSnNaA?pwd=xpi4 提取码:xpi4
- 将压缩包解压文件存放目录为 /opt/software,为方便可以在解压后进入 /opt/software目录将apache-tomcat-10.0.27重命名为tomcat:
tar -zxvf apache-tomcat-10.0.27.tar.gz -C /opt/software/
- 修改tomcat配置文件
进入conf目录,修改server.xml文件,可以修改端口号,这里修改为8088,也可以不修改。
在Host标签中之中加入一个Context标签,表示通过/basePath路径可以访问basePath文件夹下的所有文件:
<Context docBase="basePath" path="/basePath" reloadable="true" />
此时还需要在webapps下创建名为“basePath”的文件夹
进入conf目录,修改web.xml文件
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>readonly</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
- 开放端口并重启防火墙
firewall-cmd --zone=public --add-port=8088/tcp --permanent
firewall-cmd --reload
- 进入bin目录启动tomcat
sh startup.sh
如此完成了Tomcat文件服务器的搭建
1.3 引入依赖
java代码需要使用spring boot 环境且需要引入第三方jar包,详情请查看:https://blog.csdn.net/weixin_45061924/article/details/128251786
2. 相关代码及实现
2.1 测试文档
现需要在标题右侧插入一个二维码,手机扫码可预览文件
2.2 相关代码
private static void addQrcode() throws IOException {
//创建PdfDocument对象并读取文件
Document doc = new Document();
doc.loadFromFile("D:\\桌面\\测试文档.docx");
//使用Spire.Barcode的BarcodeSettings和BarcodeGenerator类创建二维码并保存为图片
BarcodeSettings settings = new BarcodeSettings();
settings.setType(BarCodeType.QR_Code);
// 设置二维码存储的内容(文件存储位置)
settings.setData("http://IP地址:8088/basePath/测试文档.pdf");
settings.setShowText(false);//设置条码数据不显示
settings.setX(0.5f);
settings.setLeftMargin(0);
settings.setRightMargin(0);
settings.setTopMargin(0);
settings.setBottomMargin(0);
settings.setQRCodeECL(QRCodeECL.Q);
settings.setQRCodeDataMode(QRCodeDataMode.Numeric);
BarCodeGenerator generator = new BarCodeGenerator(settings);
// 将生成的二维码图片转成流
Image image = generator.generateImage();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write((RenderedImage) image, "png", os);
ByteArrayInputStream inputStream = new ByteArrayInputStream(os.toByteArray());
//创建 DocPicture 类的对象
DocPicture picture = new DocPicture(doc);
//从流中加载图片
picture.loadImage(inputStream);
//设置图片的大小
picture.setWidth(25);
picture.setHeight(25);
//将图片的文本环绕方式设置为浮于文字上方
picture.setTextWrappingStyle( TextWrappingStyle.In_Front_Of_Text);
//将图片插入到第一段右上角
doc.getSections().get(0).getParagraphs().get(0).getChildObjects().insert(0,picture);
//设置图片的位置
picture.setHorizontalPosition(380.0F);
picture.setVerticalPosition(1.0F);
// 保存文档为pdf pdf格式预览,若是word格式浏览器会进行下载
doc.saveToFile("D:\\桌面\\测试文档.pdf", FileFormat.PDF);
}
此代码是在windows环境下运行的,如果项目部署到linux可以将文档直接保存至basePath目录下,当然读取文件的地址也要改变。
doc.saveToFile("/opt/software/tomcat/webapps/basePath", FileFormat.PDF);
2.3 相关结果
生成的pdf:
因为代码是在windows下运行的,所有要将生成的测试文档.pdf上传至tomcat下的webapps目录下的basePath目录下。
手机扫码结果:
二维码预览
由于服务器未连通外网,故你们扫码无法预览文件,但是相关方法亲测有效,如果有什么问题可私信或评论区。