SDKs overview
Html2PDF provides Software Development Kits (SDKs) and libraries designed to facilitate the integration of its HTML to PDF conversion service into various applications. These tools abstract the underlying RESTful API, allowing developers to interact with the service using familiar language constructs rather than direct HTTP requests. The primary function of these SDKs is to streamline the process of submitting HTML content and receiving a PDF document in return, supporting capabilities such as custom headers, footers, page sizing, and other PDF rendering options. The availability of SDKs in multiple popular programming languages aims to reduce development time and complexity for tasks like automated report generation, invoice creation, and the conversion of web content for print or archival purposes.
Html2PDF's API is designed to accept HTML, CSS, and JavaScript as input, rendering it into a high-quality PDF. The SDKs provide methods to pass this input, along with configuration parameters, to the Html2PDF service. This approach ensures consistency in PDF output regardless of the originating application's programming language. Developers can refer to the official Html2PDF API reference for detailed information on available parameters and response formats. SDKs typically handle aspects such as API key authentication, request formatting, and response parsing, enabling developers to focus on their application's core logic.
Official SDKs by language
Html2PDF maintains official SDKs for several programming languages, providing tested and supported interfaces to its HTML to PDF conversion API. These SDKs are developed and updated by Html2PDF to ensure compatibility with the latest API versions and features. They typically include examples and comprehensive documentation to assist developers in getting started quickly. The official offerings prioritize stability, security, and ease of use, making them the recommended choice for integrating Html2PDF services.
The following table outlines the official SDKs, their corresponding package managers, and typical installation commands:
| Language | Package Manager/Repository | Install Command (Example) | Maturity |
|---|---|---|---|
| PHP | Composer | composer require html2pdf/html2pdf |
Stable |
| Node.js | npm | npm install @html2pdf/node-sdk |
Stable |
| Python | pip | pip install html2pdf-python |
Stable |
| Ruby | RubyGems | gem install html2pdf-ruby |
Stable |
Each SDK provides a programmatic interface to interact with the Html2PDF service, allowing developers to submit HTML content and receive PDF documents. The Html2PDF documentation portal contains specific guides for each language, including setup instructions and usage examples. Developers are encouraged to refer to these resources for the most up-to-date information regarding installation and configuration.
Installation
Installation of Html2PDF SDKs typically follows standard practices for each respective programming language's ecosystem. Using a package manager simplifies dependency management and ensures that the correct version of the SDK is installed. After installation, developers will need to configure the SDK with their Html2PDF API key, which is usually obtained from their Html2PDF account dashboard. This key authenticates requests made through the SDK to the Html2PDF service.
PHP Installation
For PHP projects, Composer is the recommended package manager. To install the Html2PDF PHP SDK, navigate to your project's root directory in the terminal and execute the following command:
composer require html2pdf/html2pdf
This command adds the SDK to your composer.json file and downloads the necessary files into your vendor/ directory. After installation, you can include the Composer autoloader in your PHP script to make the SDK classes available.
Node.js Installation
Node.js developers can install the Html2PDF SDK using npm (Node Package Manager). In your project directory, run:
npm install @html2pdf/node-sdk
This command will add the @html2pdf/node-sdk package to your package.json dependencies. Once installed, you can import the module into your JavaScript or TypeScript files.
Python Installation
Python projects typically use pip for package management. To install the Html2PDF Python SDK, open your terminal and run:
pip install html2pdf-python
This command downloads and installs the package and its dependencies. You can then import the html2pdf module in your Python scripts.
Ruby Installation
For Ruby applications, RubyGems is the standard package manager. To install the Html2PDF Ruby SDK, execute the following command in your terminal:
gem install html2pdf-ruby
After installation, you can require the gem in your Ruby code to start using its functionalities. For more detailed instructions on setting up development environments for these languages, resources like the Mozilla Developer Network's web documentation can provide foundational information on web technologies.
Quickstart example
This section provides a basic quickstart example using the Node.js SDK to convert a simple HTML string into a PDF document. The process involves initializing the SDK client with an API key, defining the HTML content, and then calling the conversion method.
Node.js Quickstart
First, ensure you have installed the Node.js SDK as described in the installation section:
npm install @html2pdf/node-sdk
Then, create a JavaScript file (e.g., convert.js) and add the following code. Replace 'YOUR_API_KEY' with your actual Html2PDF API key, which can be found in your Html2PDF account.
const html2pdf = require('@html2pdf/node-sdk');
const fs = require('fs');
const apiKey = 'YOUR_API_KEY'; // Replace with your actual API key
const client = new html2pdf.Client(apiKey);
const htmlContent = `
<!DOCTYPE html>
<html>
<head>
<title>My PDF Document</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
h1 { color: #333; }
p { line-height: 1.6; }
</style>
</head>
<body>
<h1>Hello from Html2PDF!</h1>
<p>This is a sample HTML content converted to PDF using the Html2PDF Node.js SDK.</p>
<p>The date of conversion is: ${new Date().toLocaleDateString()}.</p>
</body>
</html>
`;
async function convertHtmlToPdf() {
try {
const pdfBuffer = await client.convert({ html: htmlContent });
fs.writeFileSync('output.pdf', pdfBuffer);
console.log('PDF generated successfully: output.pdf');
} catch (error) {
console.error('Error converting HTML to PDF:', error.message);
}
}
convertHtmlToPdf();
To run this example, save the file and execute it from your terminal:
node convert.js
Upon successful execution, a file named output.pdf will be created in the same directory, containing the rendered HTML content. This basic example can be extended to include more advanced options available through the SDK, such as specifying page size, margins, headers, and footers, as detailed in the Html2PDF API documentation.
Community libraries
While Html2PDF provides official SDKs, the broader developer community may also contribute open-source libraries or integrations that extend its functionality or provide alternative ways to interact with the service. These community-driven projects can sometimes offer support for niche use cases, different programming paradigms, or integration with specific frameworks that are not directly covered by the official SDKs.
It is important to note that community libraries are independently developed and maintained. Their stability, security, and feature parity with the latest Html2PDF API may vary. Developers considering community libraries should review their source code, check for active maintenance, and consult community feedback. The Html2PDF documentation primarily focuses on official SDKs, but developers may find community contributions by searching package repositories like npm, PyPI, or GitHub for html2pdf related projects. For example, while not directly related to Html2PDF, projects like Google's Puppeteer illustrate how open-source tools can interact with headless browsers to generate PDFs, a concept that might inspire community wrappers for services like Html2PDF.
Community contributions can be a valuable resource for developers seeking specific integrations or highly customized workflows. However, for critical production environments, relying on officially supported SDKs is generally recommended due to guaranteed compatibility, ongoing maintenance, and direct support from Html2PDF.