File Name

Hi

So I’ve tried to create a document using an InputStream, but get I try render the docs on a dataTable (I’m using java/jee and jsf) I get a blank when using the getFileName function. I saw someone used a C# where they tried this

Document doc = new Document(myInputStream);
FileSpecification fileSpec = new FileSpecification(myInputStream, fileName);
doc.EmbeddedFiled.add(fileSpec);

I had to try it with doc.getEmbeddedFiles.add(fileSpec), but no luck.

Any ideas?

@mosekgetho

The second parameter of FileSpecification() constructor is file description, not file name. It expects a String Type for the description of file which is being added as attachment to the PDF and it can be retrieved using FileSpecification.getDescription() method. Would you please explain a bit more about your use case and share your sample code snippet along with sample source file(s). Please also share the expected results that you want to achieve. We will further proceed to assist you accordingly.

Hi @asad.ali

Thanks for the response. Essentially what was to achieve is upload a document using the web browser, encrypt it with a password, then add it (for now) onto a list for later download.
The MVP for now is just uploading the file, password protecting it, then downloading it

//jsf upload page -> subject to change to use primefaces instead of tomahawk

 <h:panelGrid columns="2">

  <h:outputLabel value="Select File"/>
  <t:inputFileUpload id="file" value="#{bean.uploadedFile}" required="true"/>

  <h:outputLabel  value="File Name"/>
  <h:inputText id="fileName" value="#{bean.fileName}" required="true"/>

  <h:outputLabel value="Password"/>
  <h:inputText id="password"value="#{bean.password}" required="true"/>
 </h:panelGrid>

<h:commandButton value="Upload" action="bean.upload"/>

//the bean

public void uploadFile() throws IOException {

Document document = new Document(uploadedFile.getInputStream());

document.encrypt(password, password, 0, CryptoAlgorithm.AESx256);

download(document);

}

public void download(Document document) throws IOException{

FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();

OutputStream responseOutputStream = response.getOutputStream();

  response.reset();
  response.setHeader("Content-Type", "application/pdf");
  response.setHeader("Content-Disposition", String.format("attachment; filename=%1s_protected.pdf", getFileName()));

  document.save(responseOutputStream);

  responseOutputStream.flush();
  responseOutputStream.close();
  context.responseComplete();

}

So firstly, when out of the context of the environment I have to work, I can use the pathToInputFIle or pathToOutputFile ie new Document(pathToInputFile) and document.save(pathToOutputFile) and this will work the way I expect. In my work environmment, I’ll need to pass in the stream to create the document and download it as shown above. firstly the document.getFileName() function returns a blank and after download the pdf, it is password protected as expected, but the file is blank.

Does this explain it a bit better?

@mosekgetho

document.getFileName() is giving blank because the Document was not initialized with a file name but stream. So, you can use any file name in string format to send the browser to download file with.

Furthermore, the file could be blank due to incomplete bytes in output stream. Could you kindly share a sample web application in zip format with us which implements the complete use case that you are implementing? We will test the scenario in our environment and address it accordingly.

Hi asad, turns out that this code was correct, there was just an unrelated issue I had to resolve. I also found a workaround for storing the filename

@mosekgetho

It is good to hear that you were able to sort your issue out. Please keep using our API and feel free to create a new topic in case you need further assistance.

1 Like

A post was split to a new topic: HTML to PDF Conversion issue after binding ftp storage