在SpringBoot项目中将pdf呈现到浏览器中的问题

时间:2018-10-11 12:14:33

标签: java spring itext thymeleaf

我正在SpringBoot中做一个个人项目。我的意图是使用itext(5.0.6)创建pdf文件并在浏览器中显示它。无论我多么努力,我都会不断失败!当我点击网址时,浏览器什么也没有显示!但是当我在浏览器中打开html文件时,它显示了pdf文件!!!与百里香模板引擎有关系吗?

我尝试了google搜索,但结果对我来说不起作用或麻烦。我正在提供我的代码和结果图像,以便更好地理解。

提前谢谢。

showPdf.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Display Pdf</title>
</head>
<body>
<div>
    <p>Yor Pdf Is Shown Here!</p>
    <embed src="E:/Java/ParagraphTest.pdf" width="500" height="375" type='application/pdf'>
</div>
</body>
</html>

getPdfController.java

package com.example.test.Controller;
import com.example.test.itext.Doc;
import com.example.test.itext.Loan;
import com.example.test.itext.ParagraphPdf;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class GetPdfController {

    @GetMapping("/show")
    public String getPdf(){
        ParagraphPdf c1=new ParagraphPdf();
        c1.m1();
        return "showPdf";
    }
}

Paragraph.java

package com.example.test.itext;


import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class ParagraphPdf {
    public void m1(){
        Document document=new Document();

        try{
            PdfWriter.getInstance(document, new FileOutputStream("ParagraphTest.pdf"));

            document.open();

            Paragraph p1=new Paragraph();
            Paragraph p2=new Paragraph();
            Paragraph p3=new Paragraph();

//            p1.setSpacingBefore(10);
            p1.setAlignment(Element.ALIGN_LEFT);
            p1.setIndentationRight(30);

            p2.setSpacingBefore(30);
            p2.setAlignment(Element.ALIGN_CENTER);
            p2.setIndentationLeft(20);
            p2.setIndentationRight(20);
            p2.setSpacingAfter(30);

            p3.setAlignment(Element.ALIGN_RIGHT);
            p3.setIndentationLeft(30);

            for (int i=1;i<=20;i++){
                Chunk chunk=new Chunk("This is sentence "+i+".");
                p1.add(chunk);
                p2.add(chunk);
                p3.add(chunk);
            }

            document.add(p1);
            document.add(p2);
            document.add(p3);
            document.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>test</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.0.6</version>
        </dependency>

        <!-- flying saucer pdf -->
        <dependency>
            <groupId>org.xhtmlrenderer</groupId>
            <artifactId>flying-saucer-pdf</artifactId>
            <version>9.1.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

任何帮助或建议,将不胜感激。

通过springboot执行时:

enter image description here

使用浏览器打开html文件

enter image description here

0 个答案:

没有答案