一、引用依赖
<dependency>
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-core</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>RELEASE</version>
</dependency>
二、代码逻辑
/**
* @param fileName(文件名称)
* @param minioUrl(API地址)
* @param minioAccessKey(AccessKey)
* @param minioSecretKey(SecretKey)
* @param bucketName(桶名称)
*/
public static void main(String[] args) {
String htmlContent = "<html><head></head><body><h1>Hello World!测试中文</h1>
<p>This is an example of HTML to PDF conversion. 测试中文</p></body></html>";
// 将HTML转换为PDF
ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
PdfRendererBuilder.newBuilder()
.withHtmlContent(htmlContent, null)
.toStream(pdfOutputStream);
// 创建MinIO客户端
try (MinioClient minioClient = new MinioClient.Builder()
.endpoint(minioUrl)
.credentials(minioAccessKey, minioSecretKey)
.build()) {
// 上传PDF到MinIO
ByteArrayInputStream pdfInputStream = new ByteArrayInputStream(pdfOutputStream.toByteArray());
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket(bucketName)
.object(fileName)
.stream(pdfInputStream, pdfInputStream.available(), -1)
.build();
minioClient.putObject(putObjectArgs);
}catch (Exception e){
e.fillInStackTrace();
}
}