При конвертации JSON в PDF не может найти не один из шрифтов.
Код:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.MapType;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import java.io.File;
import java.util.Map;
public class PdfApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
ObjectMapper mapper = new ObjectMapper();
// enable pretty printing
mapper.enable(SerializationFeature.INDENT_OUTPUT);
// read map from file
MapType mapType = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
Map<String, Object> map = mapper.readValue(jsonFile, mapType);
// generate pretty JSON from map
String json = mapper.writeValueAsString(map);
// split by system new lines
String[] strings = json.split(System.lineSeparator());
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.setFont(PDType1Font.COURIER, 12);
contentStream.beginText();
contentStream.setLeading(14.5f);
contentStream.newLineAtOffset(25, 725);
for (String string : strings) {
contentStream.showText(string);
// add line manually
contentStream.newLine();
}
contentStream.endText();
contentStream.close();
document.save("pdfBoxHelloWorld.pdf");
document.close();
}
}
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.MapType;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import java.io.File;
import java.util.Map;
public class PdfApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
ObjectMapper mapper = new ObjectMapper();
// enable pretty printing
mapper.enable(SerializationFeature.INDENT_OUTPUT);
// read map from file
MapType mapType = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
Map<String, Object> map = mapper.readValue(jsonFile, mapType);
// generate pretty JSON from map
String json = mapper.writeValueAsString(map);
// split by system new lines
String[] strings = json.split(System.lineSeparator());
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.setFont(PDType1Font.COURIER, 12);
contentStream.beginText();
contentStream.setLeading(14.5f);
contentStream.newLineAtOffset(25, 725);
for (String string : strings) {
contentStream.showText(string);
// add line manually
contentStream.newLine();
}
contentStream.endText();
contentStream.close();
document.save("pdfBoxHelloWorld.pdf");
document.close();
}
}
2019-03-23 19:48:30.701 ERROR 7700 --- [nio-8443-exec-2] o.a.p.p.font.FileSystemFontProvider : Could not load font file: C:WindowsFONTSmstmc.ttf
Сразу останавливаюсь и скажу что я перепробовал практически все шрифты.
При проходе отладчиком программа валится что не странно на
Код:
contentStream.setFont(PDType1Font.COURIER, 12);
ключ1 значение
ключ2 значение
ключ3 значение
Т.е сделать JSON хоть немного читабельней.