📜 ⬆️ ⬇️

PostgreSQL Recipes: Convert from HTML to PDF

To make the conversion from HTML to PDF, we need postgres itself and its pg_wkhtmltopdf extension. (I gave links to my postgres fork, because I made some changes that I couldn’t get into the original repository yet. You can also use a ready-made way .)

To begin, install the extension command

CREATE EXTENSION pg_wkhtmltopdf 

Well, the result of the generation can, for example, be sent to the mail using pg_curl so

 CREATE OR REPLACE FUNCTION send(url TEXT, username TEXT, password TEXT, subject TEXT, "from" TEXT, "to" TEXT[], data TEXT, type TEXT) RETURNS TEXT LANGUAGE SQL AS $BODY$ WITH s AS (SELECT pg_curl_easy_reset(), --  (  ) pg_curl_easy_setopt('CURLOPT_URL', url), --    pg_curl_easy_setopt('CURLOPT_USERNAME', username), --   pg_curl_easy_setopt('CURLOPT_PASSWORD', password), --   pg_curl_recipient_append("to"), --   pg_curl_header_append('Subject', subject), --   pg_curl_header_append('From', "from"), --   pg_curl_header_append('To', "to"), --   pg_curl_mime_data(data, type:=type), --   wkhtmltopdf_set_global_setting('orientation', 'Landscape'), --    wkhtmltopdf_set_object_setting('page', 'data:text/html,<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/></head><body><p style="background-color: #c11">, !</p></body></html>'), --    HTML  PDF pg_curl_mime_data(wkhtmltopdf_convert(), file:='=?utf-8?B?'||encode(' HTML  PDF.pdf', 'base64')||'?=', type:='application/pdf', code:='base64'), --    HTML  PDF wkhtmltopdf_set_object_setting('page', 'ya.ru'), --    URL  PDF pg_curl_mime_data(wkhtmltopdf_convert(), file:='=?utf-8?B?'||encode(' URL  PDF.pdf', 'base64')||'?=', type:='application/pdf', code:='base64'), --    URL  PDF pg_curl_header_append('Connection', 'close'), --    pg_curl_easy_perform(), --  pg_curl_easy_getinfo_char('CURLINFO_HEADERS') --   ) SELECT pg_curl_easy_getinfo_char FROM s; --   $BODY$; 

And all this can be done asynchronously in the background using the scheduler .

')

Source: https://habr.com/ru/post/458986/


All Articles