Image output

Facebook screenshot

Create a screenshot of a Facebook post.

With this HTML you can generate a screenshot from any public Facebook post.

This uses the Facebook embed code. Simply swap the link in the HTML with the link to a Facebook post and it will show it.

Take note of the selector and ms_delay parameters. They must be set correctly for this image to render properly.

Preview of Facebook screenshot

Interactive example

Edit and render this image

Change the HTML, CSS, or API options. The browser preview updates as you edit; select Image to render the final output through the API.

Example code

Use this request in your app

Choose a language and copy a complete request using this example's HTML, CSS, and API options.

require "json"
require "net/http"
require "uri"

request = {
    html: <<~'HCTI_HTML',
          <div id="fb-root"></div>
          <script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v11.0" nonce="SOhYrMAR"></script>
          
          <!-- See data-href below. Replace that with the link to your Facebook post -->
          <!-- set ms_delay: 2500 to give it extra time toload -->
          <!-- set selector: .fb-post -->
          <div class="fb-post" data-href="https://www.facebook.com/HermanMiller/photos/a.171562157561/10158261466927562" data-width="500" data-show-text="true"></div>
          
          HCTI_HTML
    ms_delay: 2500,
    render_when_ready: false,
    selector: ".fb-post",
}

uri = URI("https://hcti.io/v1/image")
http_request = Net::HTTP::Post.new(uri)
http_request.basic_auth(ENV.fetch("HCTI_API_ID"), ENV.fetch("HCTI_API_KEY"))
http_request["Content-Type"] = "application/json"
http_request.body = JSON.generate(request)

response = Net::HTTP.start(
    uri.hostname,
    uri.port,
    use_ssl: true,
    open_timeout: 10,
    read_timeout: 30
) { |http| http.request(http_request) }

unless response.is_a?(Net::HTTPSuccess)
    raise format("HCTI request failed (%s): %s", response.code, response.body)
end

result = JSON.parse(response.body)
puts result.fetch("url")

Request configuration

Options used in this example

These values are sent with the HTML and CSS to control how HCTI loads the page and captures the final screenshot. You can change each one in the editor above.

Selector

selector
.fb-post
A CSS selector for an element in the HTML. We’ll crop the image to this specific element.
Learn about this option

Ms Delay

ms_delay
2500
Adds extra time in milliseconds before taking the screenshot so JavaScript can execute.
Learn about this option

From code to screenshot

How HCTI examples work

HTML/CSS to Image renders HTML and CSS in hosted Chrome, then captures the result as an image or PDF. Each example combines real source code with the API options used for the render, so it is both a working demo and a starting point for your own request.

Edit the source and options above to test changes in the browser, then render through the API to see the production result.

Common questions

Can I use the HTML and CSS from these examples?

Yes. Open an example to inspect and edit its complete HTML, CSS, and render options. Use it as a starting point, then send the resulting request from your application.

What is the difference between an example and a template?

Examples are complete API requests built from HTML and CSS. Templates are saved, reusable designs created in the visual editor and rendered repeatedly with different variable values.

Does HCTI create screenshots and PDFs?

Yes. HCTI can render HTML and CSS or capture a public webpage as an image, and it can generate PDFs when you need document output instead of a screenshot.

Keep exploring

Related examples

View all examples