当前位置: 首页 > news >正文

工业信息化网站备案系统谷歌seo优化怎么做

工业信息化网站备案系统,谷歌seo优化怎么做,厚街网站建设多少钱,软件设计师是干什么的概述 本篇博客以简单的示例代码分别在Windows和Linux环境下完成Word转PDF的文档转换。 文章提供SpringBoot Vue3的示例代码。 文章为什么要分为Windows和Linux环境? 因为在如下提供的Windows后端示例代码中使用documents4j库做转换,此库需要调用命令行…

概述

本篇博客以简单的示例代码分别在Windows和Linux环境下完成Word转PDF的文档转换。

文章提供SpringBoot + Vue3的示例代码。

文章为什么要分为Windows和Linux环境?

因为在如下提供的Windows后端示例代码中使用documents4j库做转换,此库需要调用命令行工具,并且需要安装Microsoft Word,但在Linux上无法安装Microsoft Word,因此如下提供了两份后端代码。

 过程

前端传入word文件 -> 后端处理 -> 返回转换后的字节数组(byte[])

Windows后端代码

maven依赖


<dependency><groupId>com.documents4j</groupId><artifactId>documents4j-local</artifactId><version>1.0.3</version>
</dependency><dependency><groupId>com.documents4j</groupId><artifactId>documents4j-transformer-msoffice-word</artifactId><version>1.0.3</version>
</dependency>

 示例代码

// controller接口
@PostMapping("/upload")public byte[] convertDocxToPdf(@RequestParam("file") MultipartFile file) throws IOException {if (!file.getOriginalFilename().endsWith(".docx")) {throw new IllegalArgumentException("文件类型不支持");}try (InputStream docxInputStream = file.getInputStream();ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {IConverter converter = LocalConverter.builder().build();converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();return outputStream.toByteArray();}}

CentOS后端代码

maven依赖

<dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-local</artifactId><version>4.4.2</version>
</dependency>

示例代码

    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)public byte[] uploadFile(@RequestParam("file") MultipartFile file) throws IOException, OfficeException {if (file.getOriginalFilename().endsWith(".docx")) {LocalOfficeManager officeManager = LocalOfficeManager.install();try {officeManager.start();DocumentConverter converter = LocalConverter.builder().officeManager(officeManager).build();ByteArrayOutputStream out = new ByteArrayOutputStream();converter.convert(file.getInputStream()).as(DefaultDocumentFormatRegistry.DOCX).to(out).as(DefaultDocumentFormatRegistry.PDF).execute();return out.toByteArray();} finally {OfficeUtils.stopQuietly(officeManager);}} else {throw new IOException("文件类型不支持");}}

*** 当使用上方的代码进行word转pdf之后,输出的很大可能会出现中文文字不能正确显示,文字全部变成小矩形框。

这是因为在linux上没有中文字体库导致的。

 在centos7中安装中文字体库 

1.首先检查安装所需要的工具

yum -y install fontconfig

yum -y install ttmkfdir

 2.之后检查/usr/share目录是否有fonts 和 fontconfig

3.创建chinese目录,用于存放我们需要的字体

在/usr/share/fonts下创建chinese

4.下载需要的字体

我们到自己的windows电脑上查找想要的字体,访问C:\Windows\Fonts

可以搜索自己文档转换过程中需要的字体,例如:宋体

将字体拷贝放到centos的/usr/share/fonts/chinese目录中,并修改chinese目录的权限:

chmod -R 755 /usr/share/fonts/chinese

5.执行命令,生成 TrueType 字体的字体度量

ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir

6.配置刚才创建中文字体目录,使之生效即可。

vi /etc/fonts/fonts.conf 

7.执行命令,刷新字体缓存

fc-cache 

 至此,重新访问后端服务进行word转pdf会发现字体成功显示。

前端测试代码

 在此提供与后端代码配套测试的前端代码(vue3) 

<template><div><div id="my_ipt"><label for="ipt" class="upload-button">上传文件(Word 或 PDF)</label><input id="ipt" type="file" @change="uploadFile" accept=".pdf,.docx">
</div><hr><iframe v-if="fileSrc" :src="fileSrc" width="100%" height="600px"></iframe></div>
</template><script setup>
import { ref } from 'vue';
import axios from 'axios'; const fileSrc = ref(null);const uploadFile = async (event) => {const file = event.target.files[0];if (file) {if (file.type === 'application/pdf') {fileSrc.value = URL.createObjectURL(file);} else if (file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {const formData = new FormData();formData.append('file', file);const response = await axios.post('/doc/upload', formData, {responseType: 'blob',headers: {'Content-Type': 'multipart/form-data'}});fileSrc.value = URL.createObjectURL(new Blob([response.data], { type: 'application/pdf' }));} }
};
</script><style scoped>
.upload-button {background-color: #4CAF50;border: none;color: white;padding: 10px 20px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;cursor: pointer;border-radius: 5px;
}.upload-button:hover {background-color: #3e8e41;
}</style>

http://www.hkea.cn/news/372342/

相关文章:

  • 专门做金融的招聘网站四川seo选哪家
  • wordpress nginx伪静态配置拼多多seo怎么优化
  • 深圳网站开发电话惠州网络营销
  • 中宁网站建设公司商城全网推广运营公司
  • 网站文章列表如何排版郑州seo技术培训班
  • 小型b2c网站百度开户渠道商哪里找
  • 武进区住房和城乡建设局网站爱站网能不能挖掘关键词
  • APP手机端电子商务网站建设营销成功的案例
  • 公司网站引导页百度搜索关键词排名优化技术
  • 网站开发与维护学什么网站建设seo优化培训
  • 常州网站开发百度网盘电脑版官网
  • wordpress安全权限关键词优化公司哪家好
  • 银川做网站服务google play下载安卓
  • 科技型中小企业服务网安徽搜索引擎优化seo
  • 网站建设专家排名邯郸seo营销
  • 做网站一个月20g流量够吗安全又舒适的避孕方法有哪些
  • 扫二维码直接进网站怎么做怎么提交网址让百度收录
  • 柳州建设局网站广告买卖网
  • 做外贸一般上哪些网站google play谷歌商店
  • 泉州手机网站制作如何做企业产品推广
  • 徐州手机网站设计汕头网站建设优化
  • 有没有专业收费做网站优化的百度百科优化排名
  • 常州网站建设哪家便宜江西seo推广软件
  • 如何用pageadmin做网站品牌宣传策略有哪些
  • 网站免费优化软件需要优化的地方
  • 24小时学会网站建设下载厦门百度竞价开户
  • 怎样学做网站网站权重等级
  • 做网站好还是做淘宝好北京seo推广
  • 郑州门户网站建设哪家好网站首页不收录
  • 网站制作营销型哪些网站可以发广告