I build a web application.
I would like to read files from server, then generate PDF file ( with itText) then save it to the server.
I don't know how to locate the files from the server then save the file to the server.
I read from my PC and write data to my PC perfectly.
The above code works properly but just on my computer not at server.
String jspPath = "C:\\Users\\dave\\Desktop\\eclipse\\project\\";
String fileName = "CV.txt";
InputStreamReader ir = new InputStreamReader(new FileInputStream(jspPath+filename), "UTF-8");
// Then generate the PDF with iText
//and
FileOutputStream fs = new FileOutputStream(jspPath+"generated.pdf");
PdfWriter pdfWriter = new PdfWriter(fs);
PdfDocument pdfdoc = new PdfDocument(pdfWriter);
JSP path references to my folder not the link with the generated pdf.
I would like :
put CV.txt to the server and read it.
Generate pdf ( it will work).
Save the generated PDF to server
A link to the generated PDF which i can download.
Thanks in Advance
Here are few things which might help you.
You can use FormData to pass text file from Frontend to Backend.
Use ajax post call to pass data.
you will have entire file on backend in the RequestContext parameter as FileItem object. you can start reading file using InputStreamReader.
than convert it into pdf file.
you can save pdf file to java temporary directory
String temporaryDir = System.getProperty("java.io.tmpdir");
this will return path for java temporary directory and you can delete this pdf file later
you will have to create ResponseBuilder with content-type='application/pdf' to download as a pdf file and return it to the UI. read this post
Hope this information helps you to solve your issue!
Related
This is my first hands on using Java Spring boot in a project, as I have mostly used C# and I have a requirement of reading a file from a blob URL path and appending some string data(like a key) to the same file in the stream before my API downloads the file.
Here are the ways that I have tried to do it:
FileOutputStream/InputStream: This throws a FileNotfoundException as it is not able to resolve the blob path.
URLConnection: This got me somewhere and I was able to download the file successfully but when I tried to write/append some value to the file before I download, I failed.
the code I have been doing.
//EXTERNAL_FILE_PATH is the azure storage path ending with for e.g. *.txt
URL urlPath = new URL(EXTERNAL_FILE_PATH);
URLConnection connection = urlPath.openConnection();
connection.setDoOutput(true); //I am doing this as I need to append some data and the docs mention to set this flag to true.
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("I have added this");
out.close();
//this is where the issues exists as the error throws saying it cannot read data as the output is set to true and it can only write and no read operation is allowed. So, I get a 405, Method not allowed...
inputStream = connection.getInputStream();
I am not sure if the framework allows me to modify some file in the URL path and read it simultaneously and download the same.
Please help me in understanding if they is a better way possible here.
From logical point of view you are not appending data to the file from URL. You need to create new file, write some data and after that append content from file from URL. Algorithm could look like below:
Create new File on the disk, maybe in TMP folder.
Write some data to the file.
Download file from the URL and append it to file on the disk.
Some good articles from which you can start:
Download a File From an URL in Java
How to download and save a file from Internet using Java?
How to append text to an existing file in Java
How to write data with FileOutputStream without losing old data?
I want to exact some pages from a pdf file to a new one, I create a new pdf file in Java side, its path is /data/data/com. .../newone.pdf, and I pass this path to native code, password is "", I run the code below the _docDst is NULL, I don't know why, is it because the file is empty or I just simply named its extension as pdf but it is not a pdf file, anyone who can help?
auto _docDst = FPDF_LoadDocument(dstPath,password);
I am using jasper reports in my liferay portlet and I want that user download the reports directly from the output stream. I don't want to store the reports pdf file on my server.
How can I do this with the jasper report?
File pdf = File.createTempFile("output.", ".pdf");
JasperExportManager.exportReportToPdfStream(jasperPrint, new FileOutputStream(pdf));
Right now I'm doing this which generate pdf file in some directory and then I provide download link to user. But I want that user just download the pdf from direct ouptut stream.
In your portlet action:
public void serveResource(ResourceRequest request,ResourceResponse response) throws PortletException, IOException {
response.setContentType("application/application-download");
response.setProperty("Content-disposition", "attachement; filename=<some file.pdf>");
OutputStream responseStream = response.getPortletOutputStream();
jasperPrint=...
JasperExportManager.exportReportToPdfStream(jasperPrint, responseStream);
responseStream.flush();
responseStream.close();
}
Then create the resource download url like this:
Download Report
If I understood the question correctly you coud try
ByteArrayOutputStream bous = new ByteArrayOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, bous);
byte[] bytes = bous.toByteArray();
I have an xml file already being created and rendered as a PDF sent over a servlet:
TraxInputHandler input = new TraxInputHandler(
new File(XML_LOCATION+xmlFile+".xml"),
new File(XSLT_LOCATION)
);
ByteArrayOutputStream out = new ByteArrayOutputStream();
//driver is just `new Driver()`
synchronized (driver) {
driver.reset();
driver.setRenderer(Driver.RENDER_PDF);
driver.setOutputStream(out);
input.run(driver);
}
//response is HttpServletResponse
byte[] content = out.toByteArray();
response.setContentType("application/pdf");
response.setContentLength(content.length);
response.getOutputStream().write(content);
response.getOutputStream().flush();
This is all working perfectly fine.
However, I now have another PDF file that I need to include in the output. This is just a totally separate .pdf file that I was given. Is there any way that I can append this file either to the response, the driver, out, or anything else to include it in the response to the client? Will that even work? Or is there something else I need to do?
We also use FOP to generate some documents, and we accept uploaded documents, all of which we eventually combine into a single PDF.
You can't just send them sequentially out the stream, because the combined result needs a proper PDF file header, metadata, etc.
We use the iText library to combine the files, starting off with
PdfReader reader = new PdfReader(/*String*/fileName);
reader.consolidateNamedDestinations();
We later loop through adding pages from each pdf to the new combined destination pdf, adjusting the bookmark / page numbers as we go.
AFAIK, FOP doesn't provide this sort of functionality.
I have a jsp written in which i am downloading certain files... they are pdf, zip, ppt and wmv. All the file types works except wmv. I couldnt figure out problem. When i play wmv file i get following error.
Windows Media Player cannot play the file. The Player might not support the file type or might not support the codec that was used to compress the file.
In my jps i have written code as following
response.setContentType("video/x-ms-wmv");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment; filename=123.wmv;");
String fileName = "/logs/164266828.wmv";
FileInputStream input = new FileInputStream(fileName);
BufferedInputStream buf = new BufferedInputStream(input);
int readBytes = 0;
ServletOutputStream myOut = response.getOutputStream( );
while((readBytes = buf.read( )) != -1)
myOut.write(readBytes);
Any inputs or modifications would be of great help !!!
Do not use JSP to stream binary data. JSP may have corrupted it with template text (whitespace and so on outside those <% %> things). Move this code to the doGet() method of a servlet class and invoke the servlet instead of JSP.
Unrelated to the concrete problem, those files seems static files. You don't necessarily need a servlet for this if you have full control over your server. In case of for example Tomcat, you could add the folder with the static files as another <Context> to the server.xml file.
See also:
Reliable data serving
Simplest way to serve static data from outside the application server in a Java web application