- PVSM.RU - https://www.pvsm.ru -
Для приготовления преобразования из HTML в PDF нам понадобится сам postgres [1] и его расширение pg_html2pdf [2]. (Я дал ссылки на свой форк postgres, т.к. делал некоторые изменения, которые пока не удалось пропихнуть в оригинальный репозиторий. Можно также воспользоваться готовым образом [3].)
Для начала устанавливаем расширение командой
CREATE EXTENSION pg_html2pdf
Ну а результат генерации можно, например, отправлять на почту с помощью pg_curl [4] так
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
curl.pg_curl_easy_init(),
curl.pg_curl_easy_reset(),
curl.pg_curl_easy_setopt('CURLOPT_VERBOSE', 1),
curl.pg_curl_easy_setopt('CURLOPT_URL', url),
curl.pg_curl_easy_setopt('CURLOPT_USERNAME', username),
curl.pg_curl_easy_setopt('CURLOPT_PASSWORD', password),
curl.pg_curl_recipient_append("to"),
curl.pg_curl_header_append('Subject', subject),
curl.pg_curl_header_append('From', "from"),
curl.pg_curl_header_append('To', "to"),
curl.pg_curl_mime_data(data, type:=type),
curl.pg_curl_mime_data('Тест 3', file:='=?utf-8?B?'||encode('Проверка 3.txt', 'base64')||'?=', type:='text/plain; charset=utf-8', code:='base64'),
curl.pg_curl_mime_file(array['/data/Проверка.txt', '/data/Проверка 2.txt'], file:=array['=?utf-8?B?'||encode('Проверка.txt', 'base64')||'?=', '=?utf-8?B?'||encode('Проверка 2.txt', 'base64')||'?='], type:=array['text/plain; charset=utf-8', 'text/plain; charset=utf-8'], code:=array['base64', 'base64']),
curl.pg_curl_mime_data(html2pdf('<p style="background-color: #c11">Здравствуй, мир!</p>'), file:='=?utf-8?B?'||encode('Проверка 2.pdf', 'base64')||'?=', type:='application/pdf', code:='base64'),
curl.pg_curl_header_append('Connection', 'close'),
curl.pg_curl_easy_perform(),
curl.pg_curl_easy_getinfo_char('CURLINFO_HEADERS'),
curl.pg_curl_easy_cleanup()
) select pg_curl_easy_getinfo_char from s;
$body$;
И всё это можно выполнять асинхронно в фоне с помощью планировщика [5].
Автор: RekGRpth
Источник [6]
Сайт-источник PVSM.RU: https://www.pvsm.ru
Путь до страницы источника: https://www.pvsm.ru/postgresql/323065
Ссылки в тексте:
[1] postgres: https://github.com/RekGRpth/postgres
[2] pg_html2pdf: https://github.com/RekGRpth/pg_html2pdf
[3] готовым образом: https://hub.docker.com/r/rekgrpth/postgres
[4] pg_curl: https://habr.com/ru/post/456736//
[5] планировщика: https://habr.com/ru/post/456722/
[6] Источник: https://habr.com/ru/post/458986/?utm_source=habrahabr&utm_medium=rss&utm_campaign=458986
Нажмите здесь для печати.