Recetas PostgreSQL: conversión de HTML y URL a PDF y PS

Para preparar la conversión de HTML y URL a PDF y PS, necesitamos los propios postgres , el generador htmldoc y la extensión pg_htmldoc . (Di enlaces a mis tenedores, porque hice algunos cambios que aún no se han incluido en el repositorio original. También puede usar la imagen ya preparada ).

Primero, instale las extensiones con el comando
CREATE EXTENSION pg_htmldoc 

Para convertir de HTML y URL a PDF y PS, use los comandos
 SELECT file2pdf('file.html', 'file.pdf'); --  FILE  PDF      SELECT file2ps('file.html', 'file.pdf'); --  FILE  PS      SELECT file2pdf(ARRAY['file1.html', 'file2.html'], 'file.pdf'); --   FILE  PDF      SELECT file2ps(ARRAY['file1.html', 'file2.html'], 'file.pdf'); --   FILE  PS      SELECT html2pdf(', !', 'file.pdf'); --  HTML  PDF      SELECT html2ps(', !', 'file.pdf'); --  HTML  PS      SELECT html2pdf(ARRAY[', !', ' , !'], 'file.pdf'); --   HTML  PDF      SELECT html2ps(ARRAY[', !', ' , !'], 'file.pdf'); --   HTML  PS      SELECT url2pdf('https://google.com', 'file.pdf'); --  URL  PDF      SELECT url2ps('https://google.com', 'file.pdf'); --  URL  PS      SELECT url2pdf(ARRAY['https://google.com', 'https://google.ru'], 'file.pdf'); --   URL  PDF      SELECT url2ps(ARRAY['https://google.com', 'https://google.ru'], 'file.pdf'); --   URL  PS      

Además, el resultado de la generación se puede enviar por correo usando pg_curl
 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), --   pg_curl_mime_data(file2pdf('file.html'), file:='=?utf-8?B?'||encode(' FILE  PDF.pdf', 'base64')||'?=', type:='application/pdf', code:='base64'), --    FILE  PDF pg_curl_mime_data(file2ps('file.html'), file:='=?utf-8?B?'||encode(' FILE  PS.ps', 'base64')||'?=', type:='application/ps', code:='base64'), --    FILE  PS pg_curl_mime_data(file2pdf(ARRAY['file1.html', 'file2.html']), file:='=?utf-8?B?'||encode('  FILE  PDF.pdf', 'base64')||'?=', type:='application/pdf', code:='base64'), --     FILE  PDF pg_curl_mime_data(file2ps(ARRAY['file1.html', 'file2.html']), file:='=?utf-8?B?'||encode('  FILE  PS.ps', 'base64')||'?=', type:='application/ps', code:='base64'), --     FILE  PS pg_curl_mime_data(html2pdf(', !'), file:='=?utf-8?B?'||encode(' HTML  PDF.pdf', 'base64')||'?=', type:='application/pdf', code:='base64'), --    HTML  PDF pg_curl_mime_data(html2ps(', !'), file:='=?utf-8?B?'||encode(' HTML  PS.ps', 'base64')||'?=', type:='application/ps', code:='base64'), --    HTML  PS pg_curl_mime_data(html2pdf(ARRAY[', !', ' , !']), file:='=?utf-8?B?'||encode('  HTML  PDF.pdf', 'base64')||'?=', type:='application/pdf', code:='base64'), --     HTML  PDF pg_curl_mime_data(html2ps(ARRAY[', !', ' , !']), file:='=?utf-8?B?'||encode('  HTML  PS.ps', 'base64')||'?=', type:='application/ps', code:='base64'), --     HTML  PS pg_curl_mime_data(url2pdf('https://google.com'), file:='=?utf-8?B?'||encode(' URL  PDF.pdf', 'base64')||'?=', type:='application/pdf', code:='base64'), --    URL  PDF pg_curl_mime_data(url2ps('https://google.com'), file:='=?utf-8?B?'||encode(' URL  PS.ps', 'base64')||'?=', type:='application/ps', code:='base64'), --    URL  PS pg_curl_mime_data(url2pdf(ARRAY['https://google.com', 'https://google.ru']), file:='=?utf-8?B?'||encode('  URL  PDF.pdf', 'base64')||'?=', type:='application/pdf', code:='base64'), --     URL  PDF pg_curl_mime_data(url2ps(ARRAY['https://google.com', 'https://google.ru']), file:='=?utf-8?B?'||encode('  URL  PS.ps', 'base64')||'?=', type:='application/ps', code:='base64'), --     URL  PS 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$; 

Y todo esto se puede hacer de forma asíncrona en segundo plano utilizando el programador .

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


All Articles