Skip to main content

HTML to PDF

Converting HTML to PDF with code offers more control and flexibility compared to online tools. Here are some ways to achieve this:

1. Using Python Libraries:

Python provides several libraries for HTML to PDF conversion. Here are two popular options: WeasyPrint (using wkhtmltopdf): This library utilizes the powerful wkhtmltopdf tool for rendering HTML and generating PDFs. It offers fine-grained control over the conversion process. Here's an example using WeasyPrint:

Python
from weasyprint import HTML

html_file = "my_report.html"  # Replace with your HTML file path
pdf_file = "report.pdf"

HTML(filename=html_file).write_pdf(pdf_file)

print("Converted HTML to PDF successfully!")
Use code with caution.

PDFKit (using wkhtmltopdf): Similar to WeasyPrint, PDFKit leverages wkhtmltopdf. It offers a simpler API for basic conversions.

Here's an example using PDFKit:

Python
import pdfkit

url = "https://www.example.com"  # Replace with a URL or HTML file path
pdf_file = "website.pdf"

pdfkit.from_url(url, pdf_file)  # For URLs
# or
pdfkit.from_file(html_file, pdf_file)  # For local HTML files

print("Converted HTML to PDF successfully!")
Use code with caution.

2. Using JavaScript Libraries:

For client-side conversion within a web browser, JavaScript libraries like jsPDF can be used. These libraries allow generating PDFs directly from HTML content in your web page. Here's a basic example using jsPDF:


JavaScript
var doc = new jsPDF();
doc.text("Hello, World!", 10, 10);
doc.save('my_document.pdf');
Use code with caution.
Choosing the right method depends on your needs: Python libraries offer more control and flexibility for server-side conversions. JavaScript libraries are suitable for client-side conversions within web pages. Remember, these are just basic examples. Each library offers a variety of features and options for customizing the conversion process. Make sure to explore the documentation for the chosen library to get the most out of it.

3. Java

There are two popular libraries for converting HTML to PDF in Java:

IronPDF

IronPDF provides a user-friendly API for converting HTML content (strings, files, or URLs) into well-formatted PDFs. It offers excellent support for modern web standards (HTML, CSS, and JavaScript), ensuring your PDFs look close to the original HTML.

Example

import com.ironsoftware.ironpdf.*;

public class HtmlToPdf {

public static void main(String[] args) throws Exception {
  // Convert HTML string to PDF
  String htmlContent = "<h1>Hello World!</h1>";
  PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(htmlContent);

  // Save the PDF
  myPdf.saveAs("html_string.pdf");

  // Alternatively, convert an HTML file
  String htmlFilePath = "my_report.html";
  myPdf = PdfDocument.renderHtmlFileAsPdf(htmlFilePath);
  myPdf.saveAs("report.pdf");

  System.out.println("Converted HTML to PDF successfully!");
}
}

OpenPDF

OpenPDF is another well-established library for converting various data formats (including HTML) to PDF. It offers fine-grained control over the conversion process, allowing you to customize aspects like page layout, fonts, and image handling.

Example


import com.openpdf.Content;
import com.openpdf.Encoding;
import com.openpdf.PDFWriter;
import com.openpdf.exceptions.pdf.PDFException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class HtmlToPdfOpenPDF {

public static void main(String[] args) throws Exception {
String htmlFilePath = "my_report.html";

// Read and parse the HTML file using Jsoup
Document htmlDoc = Jsoup.parse(new File(htmlFilePath), Encoding.UTF_8.toString());
String htmlContent = htmlDoc.toString();

// Create a new PDFWriter instance
PDFWriter writer = new PDFWriter();

// Add the HTML content as text
writer.add(new Content(htmlContent, Content.TEXT, Encoding.UTF_8.toString()));

// Save the PDF
writer.save("report_openpdf.pdf");

System.out.println("Converted HTML to PDF successfully!");
}
}

Choosing the right library depends on your specific needs:

  • IronPDF offers a simpler API and excellent support for modern web standards for quicker conversions.
  • OpenPDF provides a more granular level of control for advanced customization.

Remember, these are just basic examples. Make sure to consult the official documentation of each library for detailed usage instructions and advanced features.

Comments

Popular posts from this blog

Apache Storm vs Apache Flink

 Apache Storm and Apache Flink are both distributed stream processing frameworks, but they have some key differences in terms of architecture, programming models, and features. Here's a comparison between Apache Storm and Apache Flink: 1. **Programming Model:**    - **Apache Storm:** Storm provides a low-level, event-driven programming model using spouts and bolts. Spouts are sources of data, and bolts are the processing units that apply transformations or analyses to the data. It is designed for building complex, directed acyclic graphs (DAGs) of processing stages.        - **Apache Flink:** Flink offers a more high-level and expressive API for stream processing. Flink's API includes a functional programming style using operations like map, flatMap, filter, and windowing operations, making it easier to express complex data transformations. 2. **Event Time Processing:**    - **Apache Storm:** Initially, Storm had challenges in handling event ...

Shell Scripts

Shell scripts $? variable: In a shell script, we can check the return status immediately after running any command to determine if command is successful or not. like echo $? if return status is 0, it indicates success,  and if the return status is non-zero, typically 1, means failure. /dev/null /dev/null is a special device file in Unix-like operating systems (including Linux) that discards all data written to it. It essentially acts as a black hole for data. When data is written to /dev/null, it simply disappears and does not consume any storage space. Here are some common use cases for /dev/null: Discarding Output: As mentioned earlier, redirecting output to /dev/null is a common way to discard unwanted output, such as diagnostic messages or verbose output, especially when running scripts or commands in the background where you don't need to see the output. command >/dev/null  # Redirects stdout to /dev/null command 2>/dev/null # Redirects stderr to /dev/null command ...

Recover lost files on Windows, free and effective

 Windows File Recovery If necessary, download and launch the app from Microsoft Store. Press the Windows key, enter Windows File Recovery in the search box, and then select Windows File Recovery. When you are prompted to allow the app to make changes to your device, select Yes. In the Command Prompt window, enter the command in the following format:  winfr source-drive: destination-drive: [/mode] [/switches] There are 2 basic modes you can use to recover files: Regular and Extensive.  Regular mode examples Recover your Documents folder from your C: drive to the recovery folder on an E: drive. Don’t forget the backslash (\) at the end of the folder.   winfr C: E: /regular /n \Users\<username>\Documents\  Recover PDF and Word files from your C: drive to the recovery folder on an E: drive.  winfr C: E: /regular /n *.pdf /n *.docx  Extensive mode examples   winfr E: C: /extensive /n *invoice*  Recover jpeg and png photos from your...