bigfunctions > html2pdf
html2pdf¶
Call or Deploy html2pdf
?
✅ You can call this html2pdf
bigfunction directly from your Google Cloud Project (no install required).
- This
html2pdf
function is deployed inbigfunctions
GCP project in 39 datasets for all of the 39 BigQuery regions. You need to use the dataset in the same region as your datasets (otherwise you may have a function not found error). - Function is public, so it can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
- You may prefer to deploy the BigFunction in your own project if you want 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). Discover the framework
Public BigFunctions Datasets:
Region | Dataset |
---|---|
eu |
bigfunctions.eu |
us |
bigfunctions.us |
europe-west1 |
bigfunctions.europe_west1 |
asia-east1 |
bigfunctions.asia_east1 |
... | ... |
Description¶
Signature
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.
Examples¶
select bigfunctions.eu.html2pdf("<h1>Love It!</h1>")
select bigfunctions.us.html2pdf("<h1>Love It!</h1>")
select bigfunctions.europe_west1.html2pdf("<h1>Love It!</h1>")
+------------------+
| pdf_base64 |
+------------------+
| JVBERi0xLjQK.... |
+------------------+
Need help using html2pdf
?
The community can help! Engage the conversation on Slack
For professional suppport, don't hesitate to chat with us.
Found a bug using 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.
For professional suppport, don't hesitate to chat with us.
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
andcustomer_id
. - Inside the
FORMAT
function, an HTML invoice template is created. Placeholders like%d
and%s
are used for dynamic data. ARRAY_AGG
andARRAY_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.
Spread the word¶
BigFunctions is fully open-source. Help make it a success by spreading the word!