Java HTML to Image Code Examples
Complete Java examples for common HTML to Image tasks. See the Java SDK guide for installation and result handling.
You can run the examples below with the displayed API credentials. Update the input URLs and filenames as needed. Each Java example can be copied and run independently.
Basic examples
Choose file, in-memory, or output-stream handling for the PNG result. Stream examples use a file stream as their destination.
Convert a URL
Save to a file
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); client.setOutputFormat("png"); client.convertUrlToFile("https://example.com/", "example.png"); } }
Keep in memory
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); client.setOutputFormat("png"); byte[] image = client.convertUrl("https://example.com/"); System.out.println(image.length); } }
Write to a stream
import com.pdfcrowd.Pdfcrowd; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); try (OutputStream output_stream = Files.newOutputStream(Path.of("example.png"))) { client.setOutputFormat("png"); client.convertUrlToStream("https://example.com/", output_stream); } } }
Convert an HTML file
Upload an existing document.html file. For local images and stylesheets, see Local assets.
Save to a file
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); client.setOutputFormat("png"); client.convertFileToFile("document.html", "document.png"); } }
Keep in memory
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); client.setOutputFormat("png"); byte[] image = client.convertFile("document.html"); System.out.println(image.length); } }
Write to a stream
import com.pdfcrowd.Pdfcrowd; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); try (OutputStream output_stream = Files.newOutputStream(Path.of("document.png"))) { client.setOutputFormat("png"); client.convertFileToStream("document.html", output_stream); } } }
Send HTML content
These examples read HTML text from document.html. In an application, pass the HTML produced by your template renderer instead.
Save to a file
import com.pdfcrowd.Pdfcrowd; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); String html = Files.readString(Path.of("document.html")); client.setOutputFormat("png"); client.convertStringToFile(html, "html.png"); } }
Keep in memory
import com.pdfcrowd.Pdfcrowd; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); String html = Files.readString(Path.of("document.html")); client.setOutputFormat("png"); byte[] image = client.convertString(html); System.out.println(image.length); } }
Write to a stream
import com.pdfcrowd.Pdfcrowd; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); try (OutputStream output_stream = Files.newOutputStream(Path.of("html.png"))) { String html = Files.readString(Path.of("document.html")); client.setOutputFormat("png"); client.convertStringToStream(html, output_stream); } } }
Convert inline HTML
Define a small HTML document directly in your code.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); String html = "<!doctype html>\n" + "<html><body><h1>Monthly report</h1></body></html>"; client.setOutputFormat("png"); client.convertStringToFile(html, "report.png"); } }
Read from an input stream
Open document.html as a binary input stream and save the PNG to document.png.
import com.pdfcrowd.Pdfcrowd; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); try (InputStream input_stream = Files.newInputStream(Path.of("document.html"))) { client.setOutputFormat("png"); client.convertStreamToFile(input_stream, "document.png"); } } }
Local assets
Include HTML and its local images, stylesheets, and scripts in a ZIP archive, keeping their relative paths.
Convert a prepared archive
Upload an existing document.zip. Select the main HTML file explicitly when the archive contains multiple documents.
Detect the main HTML file
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); client.setOutputFormat("png"); client.convertFileToFile("document.zip", "output.png"); } }
Select index.html
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); client.setOutputFormat("png"); client.setZipMainFilename("index.html"); client.convertFileToFile("document.zip", "document.png"); } }
Convert a local directory
Create document.zip from the existing document directory, then convert it to PNG.
import com.pdfcrowd.Pdfcrowd; import java.nio.file.Files; import java.nio.file.Path; import java.util.Iterator; import java.util.stream.Stream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); Path directory = Path.of("document"); try (ZipOutputStream archive = new ZipOutputStream( Files.newOutputStream(Path.of("document.zip"))); Stream<Path> files = Files.walk(directory)) { Iterator<Path> paths = files.filter(Files::isRegularFile).sorted().iterator(); while (paths.hasNext()) { Path path = paths.next(); String name = directory.relativize(path).toString() .replace('\\', '/'); archive.putNextEntry(new ZipEntry(name)); Files.copy(path, archive); archive.closeEntry(); } } client.setZipMainFilename("index.html"); client.setOutputFormat("png"); client.convertFileToFile("document.zip", "document.png"); } }
Page rendering
Set screenshot width and scale
Capture a 1280-pixel-wide viewport and scale the PNG output to 50%.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); client.setOutputFormat("png"); client.setScaleFactor(50); client.setScreenshotWidth(1280); client.convertUrlToFile("https://example.com/", "customized.png"); } }
Hide or restyle page elements
Replace the selectors with elements from your page.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); client.setCustomCss("/* Hide navigation, ads, and sidebar */\n" + "nav, .advertisement, .sidebar, footer {\n" + " display: none !important;\n" + "}\n" + "/* Adjust layout for screenshot */\n" + ".content {\n" + " width: 100% !important;\n" + " max-width: none !important;\n" + "}"); client.setOutputFormat("png"); client.convertUrlToFile("https://example.com/", "custom_css.png"); } }
Modify the page with JavaScript
This example removes widgets and expands sections by changing their CSS classes. Adjust the selectors and class names for your page.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); client.setCustomJavascript("// Remove dynamic content before conversion\n" + "document.querySelectorAll('.video-player, .chat-widget').forEach(el => {\n" + " el.remove();\n" + "});\n" + "// Expand collapsed sections\n" + "document.querySelectorAll('.collapsed').forEach(el => {\n" + " el.classList.remove('collapsed');\n" + "});"); client.setOutputFormat("png"); client.convertUrlToFile("https://example.com/", "custom_js.png"); } }
Convert a specific element
Convert only #main in an existing document.html file. Adjust the filename and selector for your document.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); client.setElementToConvert("#main"); client.setOutputFormat("png"); client.convertFileToFile("document.html", "html_part.png"); } }
Wait for a page element
Wait for #content-loaded to appear in an existing document.html file. Choose an element that appears when your content is ready.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); client.setOutputFormat("png"); client.setWaitForElement("#content-loaded"); client.convertFileToFile("document.html", "wait_element.png"); } }
Allow extra time for JavaScript
Add a delay of 2000 milliseconds. When the page has a reliable readiness marker, use Wait for a page element.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); client.setJavascriptDelay(2000); client.setOutputFormat("png"); client.convertUrlToFile("https://example.com/", "with_delay.png"); } }
Apply print stylesheets
Apply the page’s @media print rules. The page must provide print styles for this to change its appearance.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); client.setOutputFormat("png"); client.setUsePrintMedia(true); client.convertUrlToFile("https://example.com/", "print_media.png"); } }
HTML and CSS
Add these fragments to HTML you control.
Remove or hide elements
<nav class="pdfcrowd-remove">This won't appear in the screenshot</nav> <div class="pdfcrowd-hide">This is invisible but takes up space</div>
pdfcrowd-remove removes the element from the layout. pdfcrowd-hide preserves its space.
Apply styles only during conversion
<style> /* These styles only apply during image conversion */ .pdfcrowd-body h1 { font-size: 48px; } .pdfcrowd-body footer { display: none; } .pdfcrowd-body .main-content { width: 100%; } </style>
The .pdfcrowd-body prefix limits these rules to conversion; it does not change the page in a visitor’s browser.
Templates
Render a template with JSON data
Upload existing template.html and data.json files. See the template rendering guide for template syntax, data mapping, and other supported data formats.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.HtmlToImageClient client = new Pdfcrowd.HtmlToImageClient("demo", "demo"); client.setDataFormat("json"); client.setOutputFormat("png"); client.setDataFile("data.json"); client.convertFileToFile("template.html", "output.png"); } }