Skip to content

html2pdf

html2pdf(html)

Description

Convert html to pdf encoded as a base64 string.

This function is useful to combine with send_mail if you want to send a personnalized pdf to some people.

To have a beautiful pdf, we advise you to get inspired by this invoice template.

Usage

Call or Deploy html2pdf ?
Call html2pdf directly

The easiest way to use bigfunctions

  • html2pdf function is deployed in 39 public datasets for all of the 39 BigQuery regions.
  • It can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
  • (You need to use the dataset in the same region as your datasets otherwise you may have a function not found error)

Public BigFunctions Datasets

Region Dataset
eu bigfunctions.eu
us bigfunctions.us
europe-west1 bigfunctions.europe_west1
asia-east1 bigfunctions.asia_east1
... ...
Deploy html2pdf in your project

Why deploy?

  • You may prefer to deploy html2pdf in your own project to build and manage your own catalog of functions.
  • This is particularly useful if you want to create private functions (for example calling your internal APIs).
  • Get started by reading the framework page

Deployment

html2pdf function can be deployed with:

pip install bigfunctions
bigfun get html2pdf
bigfun deploy html2pdf

Examples

select bigfunctions.eu.html2pdf("\u003ch1\u003eLove It!\u003c/h1\u003e")
select bigfunctions.us.html2pdf("\u003ch1\u003eLove It!\u003c/h1\u003e")
select bigfunctions.europe_west1.html2pdf("\u003ch1\u003eLove It!\u003c/h1\u003e")
+------------------+
| pdf_base64       |
+------------------+
| JVBERi0xLjQK.... |
+------------------+

screenshot

Use cases

A use case for the html2pdf BigQuery function would be generating personalized PDF invoices or reports directly from BigQuery data. Imagine you have a table with customer order information, including items purchased, quantities, prices, and customer details. You could use a query to format this data into an HTML invoice template, then use the html2pdf function to convert this HTML into a PDF for each customer.

Here's a more concrete example:

1. Data in BigQuery:

orders table:
| order_id | customer_id | item | quantity | price | customer_name | customer_email |
|---|---|---|---|---|---|---|
| 1 | 123 | Widget A | 2 | 10 | John Doe | john.doe@email.com |
| 1 | 123 | Widget B | 1 | 25 | John Doe | john.doe@email.com |
| 2 | 456 | Widget C | 3 | 15 | Jane Smith | jane.smith@email.com |

2. BigQuery SQL with html2pdf:

SELECT
    order_id,
    customer_id,
    bigfunctions.us.html2pdf(FORMAT("""
        <!DOCTYPE html>
        <html>
        <head>
          <title>Invoice #%d</title>
        </head>
        <body>
          <h1>Invoice #%d</h1>
          <p>Customer: %s</p>
          <table>
            <tr><th>Item</th><th>Quantity</th><th>Price</th></tr>
            %s
          </table>
          <p>Total: $%d</p>
        </body>
        </html>
    """, order_id, order_id, customer_name, ARRAY_TO_STRING(
        ARRAY_AGG(
            FORMAT("""<tr><td>%s</td><td>%d</td><td>$%d</td></tr>""", item, quantity, price)
        ), ''
    ), SUM(quantity * price))) AS invoice_pdf
FROM
    `your_project.your_dataset.orders`
GROUP BY
    order_id, customer_id, customer_name;

Explanation:

  • The query groups order items by order_id and customer_id.
  • Inside the FORMAT function, an HTML invoice template is created. Placeholders like %d and %s are used for dynamic data.
  • ARRAY_AGG and ARRAY_TO_STRING are used to create the table rows of items in the invoice.
  • SUM(quantity * price) calculates the total amount.
  • Finally, bigfunctions.us.html2pdf converts the generated HTML string into a base64 encoded PDF.

3. Result:

The query will return a table with order_id, customer_id, and invoice_pdf (base64 encoded PDF) for each order. You could then use another tool or process to decode the base64 strings and store or send the PDF invoices. This could be part of a scheduled job to automatically generate and email invoices to customers.

Other Use Cases:

  • Generating personalized reports (e.g., monthly performance reports for clients).
  • Creating product catalogs with dynamic pricing and images.
  • Generating tickets or certificates with unique codes.
  • Creating dynamic presentations based on data.

This function makes it much easier to automate the generation of personalized PDF documents directly from BigQuery, without needing to export data and use external PDF generation libraries.


Need help or Found a bug?
Get help using html2pdf

The community can help! Engage the conversation on Slack

We also provide professional suppport.

Report a bug about html2pdf

If the function does not work as expected, please

  • report a bug so that it can be improved.
  • or open the discussion with the community on Slack.

We also provide professional suppport.


Show your ❤ by adding a ⭐ on