admin管理员组

文章数量:1605630

Word转PDF 并转成base64(亲测可用)

  • 摘要:
  • 控制台
  • 实现类
  • POM
  • Gradle
  • 扩展其他方式 apose
  • 控制台
  • 实现类
  • POM
  • Gradle
  • 扩展其他方式 apose

博主 默语带您 Go to New World.
个人主页—— 默语 的博客👦🏻
《java 面试题大全》
🍩惟余辈才疏学浅,临摹之作或有不妥之处,还请读者海涵指正。☕🍭
《MYSQL从入门到精通》数据库是开发者必会基础之一~
🪁 吾期望此文有资助于尔,即使粗浅难及深广,亦备添少许微薄之助。苟未尽善尽美,敬请批评指正,以资改进。!💻⌨


摘要:

  1. Word转PDF: 使用库(如Apache POI或iText)读取Word文档内容,并使用PDF相关的库(如Apache PDFBox)将其转换为PDF格式。这一步是将文档格式转换为可打印格式,方便后续处理。
  2. PDF转Base64: 使用Java的文件读取和Base64编码库,将生成的PDF文件读取为字节流,并将其编码为Base64格式的字符串。Base64编码是一种将二进制数据编码为可传输文本的方法。
  3. 代码实现: 编写Java代码,使用适当的库实现Word到PDF的转换和PDF到Base64的编码。在此代码中,您需要调用适当的库函数以及文件读取和编码功能。
  4. 错误处理: 考虑异常处理,例如捕获文件读取和转换过程中可能出现的错误。确保在代码中进行适当的异常处理以防止程序崩溃或不正常终止。
  5. 性能和效率: 在处理大文件时,确保代码的性能和效率。避免不必要的内存消耗和重复计算。
  6. 文件清理: 在完成Base64编码后,可以考虑删除生成的临时PDF文件,以保持文件系统整洁。
  7. 测试和调试: 使用不同的Word文档进行测试,确保转换和编码过程正常工作。在问题出现时,进行调试和排除。
  8. 使用场景: 将Word文档转换为PDF并编码为Base64适用于将文档转换为可嵌入网页或作为API响应传递给前端等场景。

控制台

@Override
	public String printFile(Long id) throws IOException {

		try {
			FileDypeVo fileDypeVos = fileMapper.selectFileId(id);

			String path = fileDypeVos.getFilePath();
			String name = fileDypeVos.getFileName();

			File file = new File(path + name);
			// 获取后缀
			String str = name.substring(name.lastIndexOf(".") + 1);
			// 获取前面的名字
			String str1 = name.substring(0, name.lastIndexOf(".") - 1);
			// 拼装为pdf
			String str2 = str1 + ".pdf";
			LOGGER.info("str==》" + str);
			LOGGER.info("str1===>" + str1);
			LOGGER.info("str2===>" + str2);
			LOGGER.info("file===>" + file);

			BASE64Encoder encoder = new BASE64Encoder();

			if (str.equals("pdf")) {

				FileInputStream inputFile = new FileInputStream(file);
				byte[] buffer = new byte[(int) file.length()];

				inputFile.read(buffer);
				inputFile.close();

				System.out.println("pdf预览成功");

				return encoder.encode(buffer);
			}
			if (str.equals("docx") || str.equals("doc")) {

				String sourcePath = path + name;

				String targetPath = path + str2;

				String files01 = str2;

				LOGGER.info("sourcePath==>" + sourcePath + "==>targetPath==>" + targetPath + "==files" + files01);
				File fileYan = new File(targetPath);

				// 查看是否生成过pdf了查看文件是否是存在 不存在则进入 存在不执行
				if (!fileYan.exists()) {
					ExcelPdToWord.documents4jWordToPdf(sourcePath, targetPath);
				}

				FileInputStream inputFile = new FileInputStream(fileYan);

				byte[] buffer = new byte[(int) fileYan.length()];

				inputFile.read(buffer);
				inputFile.close();

				System.out.println("pdf预览成功");

				return encoder.encode(buffer);
			} else {
				throw new ServiceException("该文档格式无法预览");
			}

		} catch (Exception e) {
			throw new ServiceException("预览失败" + e.getMessage());
			// TODO: handle exception
		}

	}

核心代码

ExcelPdToWord.documents4jWordToPdf(sourcePath, targetPath);

实现类

	 /**
     * 通过documents4j 实现word转pdf
     *
     * @param sourcePath 源文件地址 如 /root/example.doc
     * @param targetPath 目标文件地址 如 /root/example.pdf
     */
    public static void documents4jWordToPdf(String sourcePath, String targetPath) {
        File inputWord = new File(sourcePath);
        File outputFile = new File(targetPath);
        try  {
            InputStream docxInputStream = new FileInputStream(inputWord);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream)
                    .as(DocumentType.DOCX)
                    .to(outputStream)
                    .as(DocumentType.PDF).execute();
            outputStream.close();
        } catch (Exception e) {
        	LOGGER.error("[documents4J] word转pdf失败:{}"+ e.toString());
           // log.error("[documents4J] word转pdf失败:{}", e.toString());
        }
    }
	
	
	

POM


		<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>

Gradle

compile 我这边设置以他为空,各位你们可自行查看;manve库和我们这边不一样

 compile group: 'com.documents4j', name: 'documents4j-local', version: '1.0.3'
     // https://mvnrepository/artifact/com.documents4j/documents4j-transformer-msoffice-word
     compile group: 'com.documents4j', name: 'documents4j-transformer-msoffice-word', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-api', version: '1.0.3'
  // https://mvnrepository/artifact/com.documents4j/documents4j-transformer
     compile group: 'com.documents4j', name: 'documents4j-transformer', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-transformer-api', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-transformer-msoffice-base', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-util-all', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-util-conversion', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-util-transformer-process', version: '1.0.3'
     compile group: 'org.zeroturnaround', name: 'zt-exec', version: '1.8'

扩展其他方式 apose

https://yanwc.blog.csdn/article/details/128285226@[TOC](Word转PDF 并转成base64(亲测可用))

控制台

@Override
	public String printFile(Long id) throws IOException {

		try {
			FileDypeVo fileDypeVos = fileMapper.selectFileId(id);

			String path = fileDypeVos.getFilePath();
			String name = fileDypeVos.getFileName();

			File file = new File(path + name);
			// 获取后缀
			String str = name.substring(name.lastIndexOf(".") + 1);
			// 获取前面的名字
			String str1 = name.substring(0, name.lastIndexOf(".") - 1);
			// 拼装为pdf
			String str2 = str1 + ".pdf";
			LOGGER.info("str==》" + str);
			LOGGER.info("str1===>" + str1);
			LOGGER.info("str2===>" + str2);
			LOGGER.info("file===>" + file);

			BASE64Encoder encoder = new BASE64Encoder();

			if (str.equals("pdf")) {

				FileInputStream inputFile = new FileInputStream(file);
				byte[] buffer = new byte[(int) file.length()];

				inputFile.read(buffer);
				inputFile.close();

				System.out.println("pdf预览成功");

				return encoder.encode(buffer);
			}
			if (str.equals("docx") || str.equals("doc")) {

				String sourcePath = path + name;

				String targetPath = path + str2;

				String files01 = str2;

				LOGGER.info("sourcePath==>" + sourcePath + "==>targetPath==>" + targetPath + "==files" + files01);
				File fileYan = new File(targetPath);

				// 查看是否生成过pdf了查看文件是否是存在 不存在则进入 存在不执行
				if (!fileYan.exists()) {
					ExcelPdToWord.documents4jWordToPdf(sourcePath, targetPath);
				}

				FileInputStream inputFile = new FileInputStream(fileYan);

				byte[] buffer = new byte[(int) fileYan.length()];

				inputFile.read(buffer);
				inputFile.close();

				System.out.println("pdf预览成功");

				return encoder.encode(buffer);
			} else {
				throw new ServiceException("该文档格式无法预览");
			}

		} catch (Exception e) {
			throw new ServiceException("预览失败" + e.getMessage());
			// TODO: handle exception
		}

	}

核心代码

ExcelPdToWord.documents4jWordToPdf(sourcePath, targetPath);

实现类

	 /**
     * 通过documents4j 实现word转pdf
     *
     * @param sourcePath 源文件地址 如 /root/example.doc
     * @param targetPath 目标文件地址 如 /root/example.pdf
     */
    public static void documents4jWordToPdf(String sourcePath, String targetPath) {
        File inputWord = new File(sourcePath);
        File outputFile = new File(targetPath);
        try  {
            InputStream docxInputStream = new FileInputStream(inputWord);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream)
                    .as(DocumentType.DOCX)
                    .to(outputStream)
                    .as(DocumentType.PDF).execute();
            outputStream.close();
        } catch (Exception e) {
        	LOGGER.error("[documents4J] word转pdf失败:{}"+ e.toString());
           // log.error("[documents4J] word转pdf失败:{}", e.toString());
        }
    }
	
	
	

POM


		<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>

Gradle

compile 我这边设置以他为空,各位你们可自行查看;manve库和我们这边不一样

 compile group: 'com.documents4j', name: 'documents4j-local', version: '1.0.3'
     // https://mvnrepository/artifact/com.documents4j/documents4j-transformer-msoffice-word
     compile group: 'com.documents4j', name: 'documents4j-transformer-msoffice-word', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-api', version: '1.0.3'
  // https://mvnrepository/artifact/com.documents4j/documents4j-transformer
     compile group: 'com.documents4j', name: 'documents4j-transformer', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-transformer-api', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-transformer-msoffice-base', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-util-all', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-util-conversion', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-util-transformer-process', version: '1.0.3'
     compile group: 'org.zeroturnaround', name: 'zt-exec', version: '1.8'

扩展其他方式 apose

https://yanwc.blog.csdn/article/details/128285226

如对本文内容有任何疑问、建议或意见,请联系作者,作者将尽力回复并改进📓;(联系微信:Solitudemind )

本文标签: 转成wordPDF