как можно преобразовывать математические формулы в svg
в общем то, я написал приложение - чат с нейросетью оно специально создано для учителей, что бы они могли генерировать проверочные работы и там же сгенерировать PDF файл. Но я столкнулся с такой проблемой, в PDF математические формулы отображаются в виде обычного текста, как можно сделать так, что бы формулы были привычного вида? хотелось бы отметить что в моём коде используется MathView некое подобие WebWiew которое способно в самом приложении корректно отображать формулы
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MyViewHolder> {
private final Context context;
private final List<Message> messageList;
private String formulaText;
public MessageAdapter(Context context, List<Message> messageList) {
this.context = context;
this.messageList = messageList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View chatView = LayoutInflater.from(context).inflate(R.layout.chat_item, parent, false);
return new MyViewHolder(chatView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Message message = messageList.get(position);
if (message.getSentBy().equals(Message.SEND_BY_ME)) {
holder.left_chat_view.setVisibility(View.GONE);
holder.right_chat_view.setVisibility(View.VISIBLE);
holder.right_chat_text_view.setText(message.getMessage());
} else {
holder.right_chat_view.setVisibility(View.GONE);
holder.left_chat_view.setVisibility(View.VISIBLE);
holder.left_chat_math_view.setDisplayText(message.getMessage());
// Устанавливаем формулу и сохраняем её текст
formulaText = message.getMessage();
holder.left_chat_math_view.setDisplayText(formulaText);
holder.copy_oplata_button.setOnClickListener(view -> {
copyToClipboard2(holder, holder.text_oplata.getText().toString());
Toast.makeText(context, "Скопировано!", Toast.LENGTH_SHORT).show();
});
// Обработчик кнопки копирования для сообщений от бота
holder.copy_left_button.setOnClickListener(view -> {
copyToClipboard(holder, formulaText); // Используем сохранённый текст
Toast.makeText(context, "Скопировано!", Toast.LENGTH_SHORT).show();
});
// Обработчик кнопки создания PDF
holder.create_pdf_button.setOnClickListener(view -> {
try {
createPdfFromHtml("TestGenerator_" + System.currentTimeMillis(), formulaText);
Toast.makeText(context, "PDF файл создан!", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(context, "Ошибка при создании PDF файла.", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
});
}
}
private void copyToClipboard2(MyViewHolder holder, String text) {
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Copied text", text);
clipboard.setPrimaryClip(clip);
}
// Метод для копирования текста в буфер обмена
private void copyToClipboard(MyViewHolder holder, String text) {
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Copied text", text);
clipboard.setPrimaryClip(clip);
}
private void createPdfFromHtml(String fileName, String htmlContent) throws Exception {
File pdfFile = new File(context.getFilesDir(), fileName + ".pdf");
ConverterProperties converterProperties = new ConverterProperties();
HtmlConverter.convertToPdf(htmlContent, new FileOutputStream(pdfFile), converterProperties);
// Проверяем, существует ли файл
if (pdfFile.exists()) {
// Получаем URI файла через FileProvider
Uri uri = FileProvider.getUriForFile(
context,
context.getPackageName() + ".provider",
pdfFile
);
// Создаем Intent для открытия PDF-файла
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(uri, "application/pdf");
// Запускаем Activity для просмотра PDF-файла
context.startActivity(intent);
} else {
Toast.makeText(context, "Файл не найден", Toast.LENGTH_LONG).show();
}
}
@Override
public int getItemCount() {
return messageList.size();
}
static class MyViewHolder extends RecyclerView.ViewHolder {
MaterialCardView left_chat_view, right_chat_view;
TextView right_chat_text_view, text_oplata;
ImageView copy_left_button, copy_oplata_button, create_pdf_button;
MathView left_chat_math_view;
public MyViewHolder(View itemView) {
super(itemView);
left_chat_view = itemView.findViewById(R.id.left_chat_view);
right_chat_view = itemView.findViewById(R.id.right_chat_view);
left_chat_math_view = itemView.findViewById(R.id.left_chat_math_view);
right_chat_text_view = itemView.findViewById(R.id.right_chat_text_view);
copy_left_button = itemView.findViewById(R.id.copy_left_button);
copy_oplata_button = itemView.findViewById(R.id.copy_oplata_button);
create_pdf_button = itemView.findViewById(R.id.create_pdf_button);
text_oplata = itemView.findViewById(R.id.text_oplata);
}
}