-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simplify API for creating PDF: now user needs to write less code to g…
…enerate PDF from HTML This commits adds 2 new apis: 1. class Html2Pdf (a single method for generating PDF from HTML) 2. renderer.createPDF(doc, os) (instead of old sequence `setDocument`, `layout`, `createPDF`)
- Loading branch information
Showing
11 changed files
with
122 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/Html2Pdf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.xhtmlrenderer.pdf; | ||
|
||
import com.lowagie.text.DocumentException; | ||
import org.w3c.dom.Document; | ||
import org.xhtmlrenderer.resource.FSEntityResolver; | ||
import org.xml.sax.SAXException; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class Html2Pdf { | ||
public static byte[] fromClasspathResource(String fileName) { | ||
URL htmlUrl = requireNonNull(Thread.currentThread().getContextClassLoader().getResource(fileName), | ||
() -> "Resource not found in classpath: " + fileName); | ||
return fromUrl(htmlUrl); | ||
} | ||
|
||
public static byte[] fromUrl(URL html) { | ||
ITextRenderer renderer = new ITextRenderer(); | ||
renderer.getSharedContext().setMedia("pdf"); | ||
renderer.getSharedContext().setInteractive(false); | ||
renderer.getSharedContext().getTextRenderer().setSmoothingThreshold(0); | ||
|
||
try { | ||
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); | ||
builder.setEntityResolver(FSEntityResolver.instance()); | ||
|
||
Document doc = builder.parse(html.toString()); | ||
return renderer.createPDF(doc); | ||
} | ||
catch (DocumentException | IOException | SAXException | ParserConfigurationException e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.