Recettes PostgreSQL: Conversion de HTML et d'URL en PDF et PS

Pour préparer la conversion de HTML et URL en PDF et PS, nous avons besoin de postgres lui-même, du générateur htmldoc et de l'extension pg_htmldoc . (J'ai donné des liens vers mes fourches, car j'ai apporté des modifications qui n'ont pas encore été entassées dans le référentiel d'origine. Vous pouvez également utiliser l' image prête à l' emploi .)

Tout d'abord, installez les extensions avec la commande
CREATE EXTENSION pg_htmldoc 

Pour convertir de HTML et URL en PDF et PS, utilisez les commandes
 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      

En outre, le résultat de la génération peut être envoyé à la messagerie en utilisant pg_curl afin
 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$; 

Et tout cela peut être fait de manière asynchrone en arrière-plan à l'aide du planificateur .

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


All Articles