Java: Converting HTML to PDF with special characters

0

I'm new to the group, and I wonder if anyone could give me a hand.

I'm trying to create a report for a web application.

I'm trying to get an html with the text and several 'tags' that I replace with data from the bank and write to a temporary file and then have it open in pdf format in the browser.

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

  

ERROR: 'Invalid byte 2 of UTF-8 stream from byte 2.'   org.xhtmlrenderer.util.XRRuntimeException: Can not load the XML resource   (using TRaX transformer).   com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException:   Invalid byte 2 of UTF-8 stream 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 is the code for the class that prints:

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();
        }
    }
    
asked by anonymous 20.12.2018 / 12:19

0 answers