0

I have byte array with html and try to escape. I convert byte array to string and replace special characters. When I make replace, my html doesnt work properly. It looks like a string and no css.

How to make properly ?

  String x= IOUtils.toString(getPdf(), "UTF-8");
  String secureX = replaceXssCharacters(x);
  return ResponseEntity.ok().contentType(
            MediaType.TEXT_HTML).body(secureX);


private String replaceXssCharacters(String value) {
if (value != null) {
  return value
          .replace("&","&")
          .replace("<", "&#60;")
          .replace(">","&#62;")
          .replace("\"","&#34;")
          .replace("'","&#39;");
}
return null;

}

2
  • getPdf() returns the content of a PDF file as a byte array? Commented May 30, 2023 at 14:25
  • yes it s byte array and content of pdf file Commented May 30, 2023 at 14:26

1 Answer 1

0

You should not treat a PDF file as text or a string. Doing so can corrupt the file. Treat it as a binary file. Also you should not return or process it as if it were an HTML. In Spring Boot the media type should be MediaType.APPLICATION_PDF_VALUE. Refer to this answer on the how to set the presentation in the browser.

Sign up to request clarification or add additional context in comments.

2 Comments

But I need to show as html
Please expand in the question exactly what output do you expect of a PDF as HTML and provide examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.