Java 如何在PDF itextpdf html2pdf中使用Arial字体
我从客户那里得到了以下代码。我用它来生成PDF,现在它生成的PDF没有呈现Java 如何在PDF itextpdf html2pdf中使用Arial字体,java,itext7,Java,Itext7,我从客户那里得到了以下代码。我用它来生成PDF,现在它生成的PDF没有呈现arial字体。因为我在java中没有任何先前的Exp,所以我很难理解,但我认为字体路径存在问题 package com.example.demo; import com.itextpdf.html2pdf.ConverterProperties; import com.itextpdf.html2pdf.HtmlConverter; import com.itextpdf.styledxmlparser.css.med
arial
字体。因为我在java中没有任何先前的Exp,所以我很难理解,但我认为字体路径存在问题
package com.example.demo;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.styledxmlparser.css.media.MediaDeviceDescription;
import com.itextpdf.styledxmlparser.css.media.MediaType;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.layout.font.FontProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) throws IOException {
SpringApplication.run(DemoApplication.class, args);
String htmlSource = getContent();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ConverterProperties converterProperties = new ConverterProperties();
FontProvider dfp = new DefaultFontProvider(true, false, false);
dfp.addFont("~/Library/Fonts/Arial.ttf");
converterProperties.setMediaDeviceDescription(new MediaDeviceDescription(MediaType.PRINT));
HtmlConverter.convertToPdf(htmlSource, outputStream, converterProperties);
byte[] bytes = outputStream.toByteArray();
File pdfFile = new File("java19.pdf");
FileOutputStream fos = new FileOutputStream(pdfFile);
fos.write(bytes);
fos.flush();
fos.close();
}
private static String getContent() {
return "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
" <meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\">\n" +
" <title>Document</title>\n" +
"</head>\n" +
"\n" +
"<body\n" +
" style=\"margin: 0;padding: 0;;font-family: arial, sans-serif;font-size: 14px;line-height: 125%;width: 100%;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;color: #222222;\">\n" +
" <table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" style=\"background: white;\">\n" +
" <tbody>\n" +
" <tr>\n" +
" <td style=\"padding: 0 35px;\">\n" +
" Sample
" </td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td style=\"padding: 0 35px;\">\n" +
" Sample
" </td>\n" +
" </tr>\n" +
" </tbody>\n" +
" </table>\n" +
"\n" +
"</body>\n" +
"\n" +
"</html>";
}
}
package com.example.demo;
导入com.itextpdf.html2pdf.ConverterProperties;
导入com.itextpdf.html2pdf.HtmlConverter;
导入com.itextpdf.styledxmlparser.css.media.MediaDeviceDescription;
导入com.itextpdf.styledxmlparser.css.media.MediaType;
导入com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
导入com.itextpdf.layout.font.FontProvider;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
导入java.io.ByteArrayOutputStream;
导入java.io.File;
导入java.io.FileOutputStream;
导入java.io.IOException;
@SpringBoot应用程序
公共类演示应用程序{
公共静态void main(字符串[]args)引发IOException{
run(DemoApplication.class,args);
字符串htmlSource=getContent();
ByteArrayOutputStream outputStream=新建ByteArrayOutputStream();
ConverterProperties ConverterProperties=新的ConverterProperties();
FontProvider dfp=新的DefaultFontProvider(真、假、假);
addFont(“~/Library/Fonts/Arial.ttf”);
converterproperty.setMediaDeviceDescription(新MediaDeviceDescription(MediaType.PRINT));
convertToPdf(htmlSource、outputStream、converterProperties);
byte[]bytes=outputStream.toByteArray();
文件pdfFile=新文件(“java19.pdf”);
FileOutputStream fos=新的FileOutputStream(Pdfile);
fos.写入(字节);
fos.flush();
fos.close();
}
私有静态字符串getContent(){
返回“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“文档\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“样本
“\n”+
“\n”+
“\n”+
“\n”+
“样本
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
"";
}
}
添加此行解决我的问题转换器属性。setFontProvider(dfp)
添加此行解决我的问题转换器属性。setFontProvider(dfp)