简体   繁体   中英

How to add a javascript button to PDF that saves data to a (png) file?

I create PDF figures with python+matplotlib and I can add a javascript alert box that shows the data from the figure when the PDF is opened.

I want to change the behavior in such a way that there is a button in the PDF. You might have seen "print" buttons in some forms. Pressing this button would ask the user the filename and save a hardcoded string to that file. Also another button could save the PDF as PNG (that can be hardcoded also).

The problem I have are privileges. The PDF-javascript-api says that silent file access is forbidden, but if a user initiates the save then it is allowed (eg with a standard save dialog).

However I cannot get the save dialog method to work. Adobe's doc.saveAs() without the arguments does not open save dialog, but complains about insufficient privileges.

I want the solution to be as portable as possible. Everything should be embedded into the PDF (one file).

EDIT:

Just found a similar but completely different approach: you can put attachments into PDFs that are like buttons and can be saved to disk:

https://tex.stackexchange.com/questions/117792/listings-copy-to-clipboard-feature

This is the current code that I have:

var nRslt = app.alert({cMsg: "save data?", nType: 3});
app.alert({cMsg: nRslt});
if (nRslt==4){
this.saveAs();
}

The button is in a form and if the result is 'OK' (nType = 4) the save dialog should be opened. This produces

NotAllowedError: Security settings prevent access to this property or method.
Doc.saveAs:4:Doc undefined:Open

The goal is to move the button to the document from the alert box and modify the content what is saved. But now the security settings are giving me a hard time.

The code provides the reason why the error gets thrown.

The doc.saveAs() method REQUIRES a valid path as argument. And, in order to work from a button, it requires higher privileges, which means that you'd have to create an application-level script, which has to be installed wherever the document should be used.

But there is another way, which does work:

if (app.alert({cMsg: "save data?", nType: 3}) == 4){
app.execMenuItem("SaveAs") ;
}

And with that, the Save File dialog opens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM