Skip to content
Snippets Groups Projects
Commit ba597ea0 authored by Petr Ježek's avatar Petr Ježek
Browse files

openPDF

parent cfa493b5
No related branches found
No related tags found
No related merge requests found
File added
......@@ -30,11 +30,6 @@
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
......@@ -54,6 +49,21 @@
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version>
</dependency>
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>1.3.26</version>
</dependency>
</dependencies>
<build>
......
package cz.cvut.fel.pro.emtt.service;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfString;
import com.lowagie.text.pdf.PdfWriter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.io.FileOutputStream;
import java.io.IOException;
@Service
@Slf4j
public class PDFService {
public void createDocument() {
log.info("creating a document ...");
var document = new Document();
try {
final PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("myDocument.pdf"));
document.open();
pdfWriter.getInfo().put(PdfName.CREATOR, new PdfString(Document.getVersion()));
document.add(new Paragraph("This is my first document!"));
Paragraph p1 = new Paragraph(new Chunk(
"This is my first paragraph. ",
FontFactory.getFont(FontFactory.HELVETICA, 10)));
p1.add("The leading of this paragraph is calculated automagically. ");
p1.add("The default leading is 1.5 times the fontsize. ");
p1.add(new Chunk("You can add chunks "));
p1.add(new Phrase("or you can add phrases. "));
p1.add(new Phrase(
"Unless you change the leading with the method setLeading, the leading doesn't change if you add text with another leading. This can lead to some problems.",
FontFactory.getFont(FontFactory.HELVETICA, 18)));
document.add(p1);
Paragraph p2 = new Paragraph(new Phrase(
"This is my second paragraph. ", FontFactory.getFont(
FontFactory.HELVETICA, 12)));
p2.add("As you can see, it started on a new line.");
document.add(p2);
Paragraph p3 = new Paragraph("This is my third paragraph.",
FontFactory.getFont(FontFactory.HELVETICA, 12));
document.add(p3);
} catch (DocumentException | IOException e) {
log.error(e.getMessage());
}
document.close();
log.info("document created successfully");
}
}
package cz.cvut.fel.pro.emtt.service;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class PDFServiceTest {
@Autowired
private PDFService service;
@Test
void test_createDocument_shouldCreateFile() {
service.createDocument();
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment