PostgreSQL-Rezepte: Konvertieren von HTML und URLs in PDF und PS

Um die Konvertierung von HTML und URL in PDF und PS vorzubereiten, benötigen wir postgres selbst, den htmldoc- Generator und die Erweiterung pg_htmldoc . (Ich habe Links zu meinen Gabeln angegeben, da ich einige Änderungen vorgenommen habe, die noch nicht im ursprünglichen Repository gespeichert wurden. Sie können auch das vorgefertigte Image verwenden .)

Installieren Sie zuerst die Erweiterungen mit dem Befehl
CREATE EXTENSION pg_htmldoc 

Verwenden Sie die Befehle, um von HTML und URL in PDF und PS zu konvertieren
 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      

Das Generierungsergebnis kann also auch mit pg_curl an Mail gesendet werden
 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$; 

Und das alles kann mit dem Scheduler asynchron im Hintergrund erfolgen.

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


All Articles