Код: Выделить всё
import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
import org.jodconverter.core.office.OfficeException;
import org.jodconverter.core.office.OfficeUtils;
import org.jodconverter.local.JodConverter;
import org.jodconverter.local.office.LocalOfficeManager;
Дело в том, что большинство из них он конвертирует правильно. Я подробно опишу все процессы преобразования:
Сначала я кодирую строковый rtf-файл в base64, поскольку исходное действие приложения вызывает другое, в котором происходит фактическое преобразование:
Код: Выделить всё
// Encoding the string to Base64
String base64Document = new String(DatatypeConverter
.printBase64Binary(stDocument.getBytes()));
Код: Выделить всё
try {
officeManager =
LocalOfficeManager.builder()
.officeHome(LIBRE_OFFICE_PATH)
.install().build();
officeManager
.start();
byte[] inputBytes = Base64.getDecoder().decode(fileInBase64);
ByteArrayInputStream input = new ByteArrayInputStream(inputBytes);
ByteArrayOutputStream output = new ByteArrayOutputStream();
JodConverter
.convert(input)
.to(output)
.as(DefaultDocumentFormatRegistry.PDF)
.execute();
pdfEnBase64 =
Base64.getEncoder()
.encodeToString(output.toByteArray());
} catch (OfficeException oe) {
Код: Выделить всё
// Decode base64
byte[] byteDocument = Base64
.decodeBase64(stDocumentPdf
.getBytes((java.nio.charset.StandardCharsets.UTF_8)));
Код: Выделить всё
var uintBytes = new Uint8Array(fileContent);
var blob = new Blob([uintBytes], { type: 'application/pdf' });
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);

Как видно, Adobe PDF Reader считает это легальным PDF-файлом, поэтому открывает его без проблем.
Нет никакой ошибки . Было бы неплохо протянуть руку помощи.
РЕДАКТИРОВАТЬ:
Я только что провел тесты, есть много документов rtf, которые конвертируются правильно. Я посмотрел на те, которые плохо конвертируются, как в вопросе, и увидел, что удаление заголовка документа решает проблему. Я выясняю, почему заголовок вызывает сбой преобразования.
Подробнее здесь: https://stackoverflow.com/questions/781 ... n-rtf-text