Dear All,

I am trying to include images into the PDF output produced in Stata. I am getting an error, which I believe I should not be getting. Consider the following example:
Code:
clear all
sysuse auto

graph matrix weight mpg price, half
// 11111111111111111111111111111111111111111111111111111111111
graph export "C:\temp\scatter.png", replace as(png)
putpdf begin
putpdf paragraph, halign(center)
putpdf image "C:\temp\scatter.png", width(4) linebreak
putpdf text ("Figure 1. Scatterplots")
putpdf save "C:\temp\example1.pdf", replace
erase "C:\temp\scatter.png"
// 22222222222222222222222222222222222222222222222222222222222
graph export "C:\temp\scatter.png", replace as(png)
putpdf begin
putpdf paragraph, halign(center)
putpdf image "C:\temp\scatter.png", width(4) linebreak
erase "C:\temp\scatter.png"
putpdf text ("Figure 1. Scatterplots")
putpdf save "C:\temp\example2.pdf", replace

display "OK"
The code builds up on the example from the documentation for pdfput. and demonstrates (as a minimal replication code) the nature of the problem.
The first method
  1. saves the image,
  2. adds it to the document,
  3. saves the document, then
  4. deletes the image file.
The second method's sequence is different
  1. saves the image,
  2. adds it to the document,
  3. deletes the image file.
  4. saves the document
The second method fails with error 198 "failed to add paragraph" on execution of the putpdf save command.

It seems that the image is not immediately embedded into the pdf document, but is delayed until the document is saved. This presents a problem for me, since my procedure needs to append a large number of graphs to a pdf document and is not ready to close it, since some other content must follow, yet the temporary images must be deleted.

In addition, this may present a problem to other users, which may be difficult to diagnose, as the following example demonstrates:
Code:
clear all
sysuse auto
putpdf begin

foreach v in weight length {
    twoway scatter price `v'
    graph export "C:\temp\scatter.png", replace as(png)
    putpdf paragraph, halign(center)
    putpdf image "C:\temp\scatter.png", width(4) linebreak
    putpdf text ("Figure X. `v'")
}
putpdf save "C:\temp\exampleZ.pdf", replace
erase "C:\temp\scatter.png"

display "OK"
In this case the code works until the end with no errors, but the resulting document is malformed, since it shows the same chart twice, despite the charts should be different.

The documentation is silent about timing of when the embedding actually occurs, so I believe it is ok to imply that it happens at the time the putpdf image command is executed, but the actual behavior is consistent with the image being embedded at saving of the document.

I am looking for a way to put the image into the pdf document and disposing of the image without finalizing the document, or a confirmation that this is not possible in Stata. (It would also be ok to save and reopen the pdf to continue adding more content, but as far as I know this is not possible, though very much desirable - just add pages to an existing PDF document, e.g. generated in another system).

Stata 16.0 for Windows.

Thank you, Sergiy