It was necessary to output a PDF file in Spring MVC. Now, when outputting Cyrillic is not displayed, it just disappears and the numbers remain. I did one article in runet. Here are the configs

dispatcher-servlet.xml <bean class="org.springframework.web.servlet.view.XmlViewResolver"> <property name="order" value="0" /> <property name="location"> <value>/WEB-INF/config/excel-pdf-config.xml</value> </property> </bean> <bean id="JSPViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="order" value="1"/> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> excel-pdf-config.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="pdfDocument" class="com.springapp.mvc.model_for_users.PDFDocument"> </bean> </beans> Контроллер @RequestMapping(value = "/pdf", method= RequestMethod.GET) public ModelAndView pdf() { List<MyOrderToHistory> myOrderToHistories = createCats(); return new ModelAndView("pdfDocument", "modelObject", myOrderToHistories); } 

And the Pdf document itself

package com.springapp.mvc.model_for_users;

 import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.Phrase; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; import com.springapp.mvc.models2.MyOrder4; import org.springframework.web.servlet.view.document.AbstractPdfView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Date; import java.util.List; import java.util.Map; public class PDFDocument extends AbstractPdfView { @Override protected void buildPdfDocument( Map<String, Object> model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception { PdfPTable table = new PdfPTable(3); PdfPCell header1 = new PdfPCell(new Phrase("Дата")); PdfPCell header2 = new PdfPCell(new Phrase("Сумма")); PdfPCell header3 = new PdfPCell(new Phrase("Заказчик")); header1.setHorizontalAlignment(Element.ALIGN_LEFT); header2.setHorizontalAlignment(Element.ALIGN_LEFT); header3.setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(header1); table.addCell(header2); table.addCell(header3); List<MyOrderToHistory> myOrderToHistories = (List<MyOrderToHistory>) model.get("modelObject"); for (MyOrderToHistory myOrderToHistory : myOrderToHistories) { table.addCell(myOrderToHistory.getName()); table.addCell(String.valueOf(myOrderToHistory.getWeight())); table.addCell(myOrderToHistory.getColor()); } document.add(table); } } 

Where can I set charset = UTF-8? help me please..

    1 answer 1

    In the Cyrillic font constructor. And then you also need to explicitly indicate its use when creating paragraphs, phrases and other text containers.

     Font font = FontFactory.getFont("DejaVuSans.ttf", "utf-8", BaseFont.EMBEDDED, 10); PdfPCell header1 = new PdfPCell(new Phrase("Дата", font)); 
    • thank you very much. - EmErIx_007
    • Hello, could you help? did as you wrote. Debugged many times everything seems to be going well, but when outputting, I still get empty cells (PdfPCell). And when I open this pdf in AdobeReader in the squares cells .. - EmErIx_007
    • Either the font is not detected, or the string is not in utf-8 encoding. - Sergey Gornostaev
    • The font finds, and the lines are also in utf-8.Font font = FontFactory.getFont ("C: \\ Users \\ zverek \\ Downloads \\ 9523.ttf", "utf-8", BaseFont.EMBEDDED, 10); - EmErIx_007
    • I can not reproduce your problem, it works for me. - Sergey Gornostaev