commit 3fa883c8ac10767f47c1258de6c503fbdd28f917
parent 85ed34594c401f5b5b582421ef2f64ecef69ef2a
Author: Tomas Hlavaty <tom@logand.com>
Date: Sat, 3 Oct 2026 22:43:51 +0200
avoid with-temp-buffer when computing pdf-stream length
Diffstat:
1 file changed, 15 insertions(+), 12 deletions(-)
diff --git a/emacs-pdf.el b/emacs-pdf.el
@@ -163,18 +163,20 @@ the end of the file ends the printable region."
`(pdf-dic /Size ,(1+ (length objs)) ,@(cdr x))))
(pdf-stream
(cl-destructuring-bind (a &rest b) (cdr x)
- (let* ((x (with-temp-buffer
- (set-buffer-multibyte nil)
- (mapc #'rec b)
- (buffer-string)))
- (n (length x)))
+ (let ((r (1+ (caar objs))))
(rec (if a
(cl-ecase (car a)
- (pdf-dic `(pdf-dic /Length ,n ,@(cdr a))))
- `(pdf-dic /Length ,n)))
+ (pdf-dic `(pdf-dic /Length (pdf-ref ,r 0) ,@(cdr a))))
+ `(pdf-dic /Length (pdf-ref ,r 0))))
(insert "\nstream")
- (insert x)
- (insert "\nendstream"))))
+ (let ((start (point)))
+ (mapc #'rec b)
+ (let ((end (point)))
+ (insert "\nendstream")
+ (insert "\nendobj")
+ (push `(,r 0 ,(point)) objs)
+ (insert (format "\n%d %d obj" r 0))
+ (rec (- end start)))))))
(pdf-obj
(cl-destructuring-bind (a b &rest c) (cdr x)
(push `(,a ,b ,(point)) objs)
@@ -359,8 +361,9 @@ ps-paper-type and ps-landscape-mode."
(pdf-footer-text i npages file-name)))))
(when text
(let ((oid1 (funcall oid))
- (oid2 (funcall oid)))
- (funcall !ref `(pdf-ref ,oid2 0))
+ (_oid2 (funcall oid)) ;; pdf-stream uses 2 oids, 1st for the stream obj and 2nd for the length obj
+ (oid3 (funcall oid)))
+ (funcall !ref `(pdf-ref ,oid3 0))
`((pdf-obj ,oid1
0
(pdf-stream nil
@@ -369,7 +372,7 @@ ps-paper-type and ps-landscape-mode."
1 0 0 1 ,x0 ,y0 Tm
,@text
ET))
- (pdf-obj ,oid2
+ (pdf-obj ,oid3
0
(pdf-dic /Type /Page
/Parent ,parent