weasyprint: html to pdf 파일 변환
weasyprint: html to pdf 파일 변환
2023-06-08 Note convert notes wkhtmltopdfWeasyPrint is a smart solution helping web developers to create PDF documents. It’s free and open source software that can be easily plugged to your applications and websites and turns simple HTML pages into gorgeous:
- [Pandoc]
Q: weasyprint 왜 저장하는 가?
minemacs 에서 지나가다 봤어. pdf 를 잘 저장하면 좋잖아. 참고 삼아 남겨. /home/junghan/sync/man/dotsamples/vanilla/minemacs/elisp/+io.el
(defcustom +html2pdf-default-backend 'wkhtmltopdf
"The default backend to convert HTML files to PDFs in `+html2pdf'."
:group 'minemacs-utils
:type '(choice
(const wkhtmltopdf)
(const htmldoc)
(const weasyprint)
(const pandoc+context)))
;;;###autoload
(defun +html2pdf (infile outfile &optional backend)
"Convert HTML file INFILE to PDF and save it to OUTFILE.
When BACKEND is provided, the corresponding program is used, otherwise, the
value of `+html2pdf-default-backend' is used."
(if-let ((default-directory (file-name-directory infile))
(backend (or backend +html2pdf-default-backend))
(backend-command
(pcase backend
('weasyprint
(list "weasyprint"
"--encoding" "utf-8"
"--stylesheet" (expand-file-name "templates/weasyprint-pdf.css" minemacs-assets-dir)
infile outfile))
('htmldoc
(list "htmldoc"
"--charset" "utf-8"
"--bodyfont" "sans" "--textfont" "sans" "--headfootfont" "sans"
"--top" "10#mm" "--bottom" "10#mm" "--right" "10#mm" "--left" "10#mm"
"--fontsize" "11"
"--size" "a4"
"--continuous"
"--outfile" outfile infile))
('wkhtmltopdf
(list "wkhtmltopdf"
"--images" "--disable-javascript" "--enable-local-file-access"
"--encoding" "utf-8"
infile outfile))
('pandoc+context
(list "pandoc"
"--pdf-engine=context"
"--variable" "fontsize=10pt"
"--variable" "linkstyle=slanted"
"-o" outfile infile))
('pandoc
(list "pandoc"
"--defaults" (expand-file-name "templates/pandoc.yaml" minemacs-assets-dir)
"-o" outfile infile)))))
(apply #'call-process (append (list (car backend-command) nil nil nil) (cdr backend-command)))
(user-error "Backend \"%s\" not available." backend)))
;;;###autoload
(defun +txt2html (infile outfile &optional mail-mode-p)
"Convert plain-text file INFILE to HTML and save it to OUTFILE.
When MAIL-MODE-P is non-nil, --mailmode is passed to \"txt2html\"."
(apply #'call-process (append '("txt2html" nil nil nil "-8")
(when mail-mode-p '("--mailmode"))
(list "--outfile" outfile infile))))
(defvar +save-as-pdf-filename nil
"File name to use, if non-nil, for the output file.")
;;;###autoload
(defun +save-as-pdf (infile &optional mail-mode-p)
"Save URL as PDF.
This function's signature is compatible with `browse-url-browser-function'
so it can be used to save HTML pages or emails to PDF.
When MAIL-MODE-P is non-nil, treat INFILE as a mail."
(let* ((infile (string-trim-left infile "file://"))
(outfile (+file-name-incremental
(or +save-as-pdf-filename
(expand-file-name
(file-name-with-extension (file-name-base infile) ".pdf")
(file-name-directory infile))))))
(if (zerop
;; For HTML files, just call `+html2pdf'
(if (string= "html" (file-name-extension infile))
(+html2pdf infile outfile)
;; For non-HTML (plain-text) files, convert them to HTML then call `+html2pdf'
(let ((tmp-html (make-temp-file "txt2html-" nil ".html")))
(+txt2html infile tmp-html mail-mode-p)
(+html2pdf tmp-html outfile))))
(message "Exported PDF to %s"
(truncate-string-to-width
(abbreviate-file-name outfile)
(/ (window-width (minibuffer-window)) 2) nil nil t))
(user-error
(if (file-exists-p outfile)
"PDF created but with some errors!"
"An error occurred, cannot create the PDF!")))))
Related-Notes
References
마지막 수정일자