Bootsrap not working with spring boot thymeleaf pdf template - spring-boot

I have a problem adding bootstrap into my thymeleaf template. I want to export that template as pdf.
Bootsrap is not applied to my template. I tried webjars and static bootsrap js and css files but it doesn't work. Is there any other way that i can do? These are webjars links that I have added inside html;
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" th:href="#{webjars/bootstrap/css/bootstrap.min.css}" type="text/css"/>
<script th:src="#{webjars/bootstrap/js/bootstrap.min.js}"></script>
<script th:src="#{webjars/jquery/jquery.min.js}"></script>
</head>
This is java code;
public byte[] getReportByTestId(Long testId, String templateName) throws IOException, DocumentException {
Optional<Test> test = testRepository.findById(testId);
if (test.isPresent()) {
Test _test = test.get();
ObjectMapper oMapper = new ObjectMapper();
Map map = oMapper.convertValue(_test, Map.class);
Context context = new Context();
context.setVariables(map);
String htmlContentToRender = templateEngine.process(templateName, context);
String xHtml = xhtmlConvert(htmlContentToRender);
ITextRenderer renderer = new ITextRenderer();
String baseUrl = FileSystems
.getDefault()
.getPath("src", "main", "resources", "templates")
.toUri()
.toURL()
.toString();
renderer.setDocumentFromString(xHtml, baseUrl);
renderer.layout();
ByteOutputStream outputStream = new ByteOutputStream();
renderer.createPDF(outputStream);
byte[] pdfAsBytes = outputStream.getBytes();
outputStream.close();
return pdfAsBytes;
}
return null;
}
private String xhtmlConvert(String html) throws UnsupportedEncodingException {
Tidy tidy = new Tidy();
tidy.setInputEncoding("UTF-8");
tidy.setOutputEncoding("UTF-8");
tidy.setXHTML(true);
ByteArrayInputStream inputStream = new ByteArrayInputStream(html.getBytes(StandardCharsets.UTF_8));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
tidy.parseDOM(inputStream, outputStream);
return outputStream.toString("UTF-8");
}
When I add like this;
<script th:src="#{../static/vendor/bootstrap5/bootstrap.min.js}"></script>
It partially works. Only colors are applied.
Thanks for any help.

Related

spring-boot-2.0.6 freemarker mvc View print request model values twice

I am using freemarker view with spring-boot-2.0.6.RELEASE,
But my ftl view print request model value twice in HTML like "${errorTitle}" -> "PG ErrorPGError" .
If anybody has a solution, please let me know the way.
I am struggling with these problems for 3 days.
[freemarker configuration bean]
#Bean
public FreeMarkerConfigurer freemarkerConfig()throws IOException, TemplateException {
final FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
freeMarkerConfigurer.setTemplateLoaderPath("classpath:/WEB-INF/view/");
Properties settings = new Properties();
settings.setProperty(
freemarker.template.Configuration.TEMPLATE_EXCEPTION_HANDLER_KEY, "rethrow");
freeMarkerConfigurer.setFreemarkerSettings(settings);
freeMarkerConfigurer.setDefaultEncoding("UTF-8");
return freeMarkerConfigurer;
}
[/paygate/error.ftl]
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Error Page</title>
</head>
<body>
<h1>${errorTitle}</h1>
<p>${errorMessage}</p>
</body>
</html>
[pom.xml]
:::
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
:::
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
#CrossOrigin
#SkipSessionCheck
#GetMapping("/error)
public ModelAndView errorView(#RequestParam("errorMessage") String errorMessage) throws ResultCodeException {
logger.error(AppUtil.excetionToString(e)) ;
ModelAndView model = new ModelAndView();
model.addObject("errorTitle", "PG Error") ;
model.addObject("errorMessage", errorMessage) ;
model.setViewName("paygate/error");
return model ;
}

How to enter angular4 pages from spring mvc

I have angular4 + spring mvc project here is my project hierarchy
my index.html content
<!DOCTYPE html><html lang="en"><head>
<meta charset="utf-8">
<title>MonitoringFront</title>
<base href="./">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
<script type="text/javascript" src="runtime.js"></script><script type="text/javascript" src="polyfills.js"></script><script type="text/javascript" src="styles.js"></script><script type="text/javascript" src="vendor.js"></script><script type="text/javascript" src="main.js"></script></body></html>
and my web Configuration class
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = { "ge.mvc"})
public class WebMvcConfig implements WebMvcConfigurer {
#Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/monitoringFront");
return resolver;
}
}
and when I want to enter my pages for example login page which is my default page I am getting page not fount error when i am trying to enter http://localhost:8080/springMvcApp/ url . Did I do something wrong ?
my router urls from angular 4 app.module.ts file
const appRoutes:Routes = [
{
path: '',
component: LoginComponent
},
{
path: 'dashboard',
component: DashboardComponent
}
]
I considere that your problem is equals with this question, because you want to render only de angular application,
How to serve .html files with Spring
In my point of view you need to split the problems to have a better architecture in one side you need your front end using angular 5 and in the other side spring mvc to create rest services, and also you probably will need JWT for authentication.

MimeMessageHelper Spring boot to send email

I am using spring boot to sent an email. Code snippet from my email service
private #Autowired JavaMailSender mailSender;
AND
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,
MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED, StandardCharsets.UTF_8.name());
helper.setTo(bo.getToEmails().parallelStream().toArray(String[]::new));
helper.setBcc(bo.getBccEmails().parallelStream().toArray(String[]::new));
helper.setCc(bo.getCcEmails().parallelStream().toArray(String[]::new));
helper.setText(htmlBody, true);
helper.setText(textBody, false);
helper.setSubject(bo.getSubject());
helper.setFrom(new InternetAddress(bo.getFromEmail(),bo.getSenderLabel()));
First I set the htmlBody and then textBody
helper.setText(htmlBody, true);
helper.setText(textBody, false);
it override htmlBody to textBody . How can I send both text and html body using org.springframework.mail.javamail.MimeMessageHelper; any update ?
You can use thymeleaf as your HTML template engine.
Sample HTML code:
MySampleHTML.html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
<title>Sample Email</title>
</head>
<body>
<div th:text="${sampleText}"></div>
</body>
<html>
Sample Java Code:
public class EmailSample {
#Autowired
private JavaMailSender mailSender;
#Autowired
private TemplateEngine templateEngine; // From Thymeleaf
public void initiateEmailSend() {
String processedHTMLTemplate = this.constructHTMLTemplate();
// Start preparing the email
MimeMessagePreparator preparator = message -> {
MimeMessageHelper helper = new MimeMessageHelper(message, MimeMessageHelper.MULTIPART_MODE_MIXED, "UTF-8");
helper.setFrom("Sample <sample#example.com>");
helper.setTo("recipient#example.com");
helper.setSubject("Sample Subject");
helper.setText(processedHTMLTemplate, true);
};
mailSender.send(preparator); //send the email
}
// Fills up the HTML file
private String constructHTMLTemplate() {
Context context = new Context();
context.setVariable("sampleText", "My text sample here");
return templateEngine.process("MySampleHTML", context);
}
}
And include thymeleaf on your pom.xml
<!-- For email HTML templating -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
NOTE: Put the MySampleHTML.html file on resources/templates/ folder for thymeleaf to see it.
instead of
helper.setText(htmlBody, true);
helper.setText(textBody, false);
Use
helper.setText(htmlBody, textBody);

JavaMail API - Send Emoji/Smiley with Freemarker Template?

I developed rest API in spring.
I have to send invitation mail to user with Emoji.
I developed .ftl and .java configuration file is as follows:
invitation.ftl
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
//Content
</body>
</html>
webAppConfi.java
#Bean
public FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean() {
FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean = new FreeMarkerConfigurationFactoryBean();
freeMarkerConfigurationFactoryBean
.setTemplateLoaderPath("/WEB-INF/templates/mail/");
freeMarkerConfigurationFactoryBean.setDefaultEncoding("UTF-8");
return freeMarkerConfigurationFactoryBean;
}
sendMail.java
MimeMessage message = null;
MimeMessageHelper objHelper = null;
String strBody = null;
try {
strBody = FreeMarkerTemplateUtils.processTemplateIntoString(
freemarkerMailConfiguration.getTemplate(templetName),
templateProp);
message = this.mailSender.createMimeMessage();
objHelper = new MimeMessageHelper(message, true);
objHelper.setTo(to);
objHelper.setText(strBody, true);
objHelper.setSubject(subject);
mailSender.send(message);
}
finally{}
I get the string like that from iPhone \ud83d\ude14\ud83d\ude17.
So how could i send this emoji value to email body part?
Thank you!

Jersey - Freemarker - use custom ViewProcessor

My template is rendered but without the freemarker treatments.
the output is
<html>
<body>
<h1>Welcome ${user}</h1>
</body>
</html>
The variable user has not been replaced by its value.
So I'm not sure Jersey know something about my custom ViewProcessor. How can I say explicitly to Jersey that I want to use the Freemarker ViewProcessor.
I have seen the following sample but it doesn't help
http://search.maven.org/#browse%7C-1697896452
To sum up I have :
in my pom.xml
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-freemarker</artifactId>
<version>1.12</version>
</dependency>
in my JerseyServletModule
Map<String, String> params = new HashMap<String, String>();
params.put("com.sun.jersey.freemarker.templateBasePath", "freemarker");
serve("/*").with(GuiceContainer.class, params);
in my TestResource
#Path("/test")
#GET
#Produces("text/html")
public Viewable getMytestView() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("user", "Peter");
return new Viewable("/test.ftl", map);
}
in my src/main/resources/freemarker/test.ftl
<html>
<body>
<h1>Welcome ${user}</h1>
</body>
</html>
Thanks in advance for your help.
Ok it works now. Sorry, it's certainly a cache problem.

Resources