Java: Converting HTML to PDF with special characters

Asked

Viewed 288 times

0

I’m new to the group, and I was wondering if someone could give me a hand.

I am trying to create a report for a web application.

I am trying to take an html with the text and several 'tags' that I replace with data from the database and write to a temporary file to have it opened in pdf format in the browser.

The problem is accents. If the string I read from the base file has accented letters, the error below occurs.

ERROR: 'Invalid byte 2 of UTF-8 sequence of byte 2.' org.xhtmlrenderer.util.Xrruntimeexception: Can’t load the XML Resource (using Trax Transformer). com.sun.org.apache.xerces.internal.impl.io.Malformedbytesequenceexception: Invalid byte 2 of UTF-8 sequence of byte 2.

But if I put accents in the base file and perform the conversion to pdf directly from it, the pdf is generated normally.

Below follows the code of the printing class:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

import org.omnifaces.util.Faces;
import org.xhtmlrenderer.pdf.ITextRenderer;

import br.com.santarita.automatizaata.domain.Ata;

public class ImpressaoUtil {
    private static File arquivoBase, arquivoTemp;
    private static Ata ata;

    public void imprimeAta(Ata ataImprimir) {
        ata = ataImprimir;
        arquivoBase = null; 
        arquivoTemp = null;
        String htmlBase = leArquivoBase();
        String htmlImpressao = escreveArquivoTemp(htmlBase);
        criaArquivoTemp(htmlImpressao);
        imprime();
        excluiArquivoTemp();
    }

    private String leArquivoBase() {
        String caminho, html;
        FileReader fileReader;
        BufferedReader buffer;

        try {
            caminho = Faces.getRealPath("/documentos/ataBase.html");
            arquivoBase = new File(caminho);
            fileReader = new FileReader(arquivoBase);
            buffer = new BufferedReader(fileReader);
            html = "";
            while (buffer.ready()) {
                String newString = buffer.readLine();
                System.out.println(newString);
                html += newString;
            }
            buffer.close();
            fileReader.close();
            return html;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            return null;
        }

    }

    private String escreveArquivoTemp(String html) {
            String htmlNovo = html;         
            System.out.println("old: "+html);
            String nome = ata.getAluno().getNome();
            while(htmlNovo.charAt(0) != '<') {
                htmlNovo = htmlNovo.substring(1, htmlNovo.length());
            }

            htmlNovo = html.replace("[nome_aluno]", nome);

            System.out.println("new: "+htmlNovo);
            return htmlNovo;
    }

    private void criaArquivoTemp(String html) {
        String caminhoTemp = arquivoBase.getAbsolutePath();
        try {
            caminhoTemp = caminhoTemp.replace("ataBase", "ataTemp");
            arquivoTemp = new File(caminhoTemp);

            if (!arquivoTemp.exists()) {
                arquivoTemp.createNewFile();
            }

            FileWriter fileWriter = new FileWriter(arquivoTemp);
            BufferedWriter buffer = new BufferedWriter(fileWriter);

            buffer.write(html);

            buffer.close();
            fileWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void imprime() {
        FacesContext facesContext = FacesContext.getCurrentInstance(); 
        ExternalContext externarContext = facesContext.getExternalContext();    
        try {
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocument(arquivoTemp);
            renderer.layout();
            HttpServletResponse response = (HttpServletResponse) externarContext.getResponse();
            response.reset();
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "inline; filename=\"print-file.pdf\"");
            OutputStream outputStream = response.getOutputStream();
            renderer.createPDF(outputStream);           
        } catch (Exception e) {
            e.printStackTrace();
        }
        facesContext.responseComplete();
    }

    private void excluiArquivoTemp() {
        if (arquivoTemp.exists()) {
            arquivoTemp.delete();
        }
    }
  • 1

    Thanks for the comment André! Mas não deu. I think the problem is in the java string... It must be changing the encoding or generating confusion. From what I researched, it would use UTF-16 encoding... I tried some things to convert to utf-8, but it generates more error still, hehe. I ensured that the base file is saved in UTF-8. So much so that if you generate the pdf directly from it, it generates without problems. But when you save a new one saving the changed string, the error occurs.

  • 1

    Ex. If I put an 'A' in the base file and generate the pdf directly from it (ensuring that the file is in UTF-8), the pdf will be generated without problems. If I read this in a string, and save this string in another file (without changing anything), the error occurs.

  • 1

    I got it. The problem really was in reading. I found this link to handle the reading: https://www.mkyong.com/java/how-to-write-utf-8-encoded-data-into-a-file-java/ Do the replacements and use the link code below to save: https://www.mkyong.com/java/how-to-read-ut-f8-encoded-datafrom-file-java/ Apparently solved the problem. Thanks for your attention anyway!!

  • 1

    Yes and No. Contenttype continued without UTF. What I changed was adjusting the entire reading and writing routine. To ensure that you would read and save in UTF-8 in the new file.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.