API Documentation > Rest API  > Convert HTML to Image/PDF with Rest API

Convert HTML to Image/PDF with Rest API

Use this REST API to convert HTML to image or pdf.

HTTP GET/POST to Call

We only support POST method of this API. To make a call, make an HTTP POST request to api server to following URL.

http://api.page2images.com/html2image

Parameters

Required Parameters

Parameter Default Description
{p2i_html} N/A The HTML code you want to convert to image or pdf. Max length is 8M.
We suggest you use inline css and javascript in the html. Of cause, you also can use the extra javascript/css/image links in the html. Our engine will try to access them automatically.
Test your html code by convert html to image or pdf online
{p2i_key} N/A The API key in your account. You can get it here.
Please use the Restful Key rather than Direct Link Key. Why?

Optional Parameters

Parameter Default Description
{p2i_url} localhost The url of this page in public network. URL is optional and used to resolve relative URLs in HTML you sent, such as referenced images, css, javascript. System will assume all of the relative URLs can be reach in public network.
{p2i_device} 6 Our system can generate the screenshot for those smart phones and desktops. Please pass it to server as needed.
Following are devices we supported:
0 - iPhone4, 1 - iPhone5, 2 - Android, 3 - WinPhone, 4 - iPad, 5 - Android Pad, 6 - Desktop, 7 - iPhone6, 8 - iPhone6+
{p2i_size} N/A The screenshot file size you want to get. Not the screen size of device. If you want a big image, you need enlarge the width and height. If you need a small one, you need reduce the width and height. But it will take some more time to download a big image than a small one.
Format:
widthxheight e.g. 300x300

We predefined the size for those devices for most usage:
Mobile Phones: 320x480
Tablets: 512x384
Desktop: 640x400

Advanced information:
1) If you only know the fixed width or height of the images you want to get, you can just pass the width or height to us by following format: 512x0 or 0x512. We will resize the image to fit that width/height and make sure it is the biggest image we can generate.
2) The screenshot image will be resized to fit the width and height without stretching
{p2i_screen} N/A The screen size of device you want to simulate.
Format:
screen_widthxscreen_height e.g. 1024x768
We predefined the size for those devices for most usage:
Mobile: 320x480
Mobile iPhone5: 320x500
Tablets: 1024x768
Desktop: 1280x800
Advanced information:
1) The screen size should not be less than image size.
{p2i_fullpage} True Full page or just the screen area. If the value is 1 or true, we will take the full page screenshot. If the value is 0 or false, we will take the screen area only.
{p2i_callback} N/A It will take around 10-30 seconds to generate one screenshot. If you want to us send a notification when we finish the screenshot of one page, you can send us your listener URL in the request. Please return 200 status code when you get the request.
How to create website screenshot with callback API
{p2i_imageformat} png png, jpg, pdf or html
We strongly recommend jpg since most jpg file is smaller than png. But png quaility is much better than jpg.
{p2i_quality} 80 The quality of the image. By default, JPG is 90 and PNG is 80.
JPG: you can change it from 80 to 95 as needed.
PNG: you can change it from 70 to 85 as needed.
{p2i_wait} 0 How many second wait after page load complete. System will not accept the value larger than 25 seconds.
{p2i_htmlerror } true True or false.
We will display the missing css, js or images errors in the top of the html if this flag is true.
{p2i_js} 1 1 or 0. 1 means enable javascript. 0 means disable javascript. If you do not want to run javascript on target page (for example, some welcome message), please use this parameter.
{p2i_csspath} null It is an URL which point to a css file. For example, http://your_domain/test.css. System will add the css styles when load the target page.

Notes:
1. It is hard to debug. But it do works. :)
2. Only chrome engine can use this field;
3. Please use !important in the properties of CSS since it is important.
4. CSS file size should less than 1 M.
5. We will not throw out any exception when your CSS file is not valid to access or it is not a valid CSS style. So, be carefull on that.


Following is an example, we change our header to a red version. Request: http://api.page2images.com/restfullink?p2i_url=http://www.page2images.com&p2i_device=6&p2i_screen=1024x768&p2i_csspath=http://api.page2images.com/test.css&p2i_key=YOUR_KEY

Possible Return Values

All of the return format is Json format which supported by most of the programming languages.

If server is processing your request, you will receive:

{
"status":"processing",
"estimated_need_time":"15"
}

If server has finished your request, you will receive:
PS: image_url is JSON encoded.

{
"status":"finished",
"image_url":"http://api.page2images.com/images/00/00/10923423.jpg",
"duration":"11",
"left_calls":"923023"
}

Then you can download and do whatever you think is right on the images. We will keep this image for you for 24 hours. After that, due to the disk limitation, we will remove that image. Of cause, if you want to generate it again, we will capture it again.

If server encounter any problem, we will return:

"p2i_html" is empty:{
"status":"error",
"errno":"403"
"msg": "detail information here"
}
generate p2i_html file failed:{
"status" => "error",
"msg" => "Sorry, generate html file error.",
"errno" => 403
}

Possible error messages are following:

Errno:403 Msg: The API Key does not exist.

Errno:403 Msg: Account does not have enough credits.

Errno:403 Msg: Account has expired.

Errno:403 Msg: You are using direct link key in Rest API.

Errno:404 Msg: We cannot access target URL.

Errno:500 Msg: Time out.

Sample Codes
Download Java sample source code for website screenshot
Download PHP sample source code for website screenshot
Download Python sample source code for website screenshot
Download PHP sample source code for convert HTML to image
Use Code Generator to Build the Code

Using our Code Generator, you can quickly generate the source code for IMG Tags. Use page2images code generator for website screenshot.

Free Engineering Service

Premium and XLarge rate plan users get free help with writing and testing code. Please contact us if you need help.

version:1.7