I am created an rest API for generate the PDF file using itext API. Please help me out how to generate this and send to UI for download that PDF.
Here I am Using Angularjs,SpringBoot and Mysql as DB.
#RequestMapping(value = "/generateGeneralLedgerReportPdf", method =
RequestMethod.GET)
public void generateSalesReportPdf(#RequestParam("ledgerStartDate")
String ledgerStartDate,
#RequestParam("ledgerEndDate") String ledgerEndDate) {
try {
SimpleDateFormat simpleDateFormat = new
SimpleDateFormat("yyyy-MM-dd");
Date startDate =
simpleDateFormat.parse(ledgerStartDate);
Date endDate = simpleDateFormat.parse(ledgerEndDate);
List<GeneralLedger> listLedgerDetails = null;
int count = 0;
File file = new File("E:\\GeneralLedgerReport.pdf");
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream(file));
document.open();
//create PDF
PdfPTable table = new PdfPTable(6); // 10 columns.
table.setWidthPercentage(100); //Width 100%
PdfPCell c1 = new PdfPCell(new Phrase("#"));
c1.setHorizontalAlignment(Element.ALIGN_LEFT);
c1.setBackgroundColor(BaseColor.GRAY);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("DATE"));
c1.setHorizontalAlignment(Element.ALIGN_LEFT);
c1.setBackgroundColor(BaseColor.GRAY);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("INCOME CATEGORY"));
c1.setHorizontalAlignment(Element.ALIGN_LEFT);
c1.setBackgroundColor(BaseColor.GRAY);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("AMOUNT"));
c1.setHorizontalAlignment(Element.ALIGN_LEFT);
c1.setBackgroundColor(BaseColor.GRAY);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("EXPENSE CATEGORY"));
c1.setHorizontalAlignment(Element.ALIGN_LEFT);
c1.setBackgroundColor(BaseColor.GRAY);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("AMOUNT"));
c1.setHorizontalAlignment(Element.ALIGN_LEFT);
c1.setBackgroundColor(BaseColor.GRAY);
table.addCell(c1);
listLedgerDetails = generalLedgerService.generateGeneralLedgerPdfByRange(startDate, endDate);
if (!listLedgerDetails.isEmpty()) {
for (GeneralLedger ledger : listLedgerDetails) {
count ++;
Double incomeAmount = ledger.getIncomeAmount();
if(incomeAmount==null) {
incomeAmount = 0.0d;
}
Double expenseAmount = ledger.getExpenseAmount();
if(expenseAmount==null) {
expenseAmount = 0.0d;
}
table.addCell(String.valueOf(count));
table.addCell(String.valueOf(ledger.getLedgerDate()));
table.addCell(ledger.getIncomeCategory());
table.addCell(String.valueOf(incomeAmount));
table.addCell(ledger.getExpenseCategory());
table.addCell(String.valueOf(expenseAmount));
}
}
document.add(table);
document.close();
writer.close();
}catch (Exception e) {
e.printStackTrace();
}
}
Angularjs
$scope.generateGeneralLedgerReportPdf = function(startDate,endDate){
$http({
url:
'service/generalLedger/generateGeneralLedgerReportPdf',
method: "GET",
params: {ledgerStartDate:startDate,ledgerEndDate:endDate}
})
.success(function(response){
console.log("Success");
})
.error(function(response) {
console.log("Failed");
});
};
It is giving me proper OUTPUT but it is storing in local system E: drive. but i want to download in browser window.
Your code to download is missing also that depends on file created is publicly available via your HTTP server or servlet container you can simply redirect to via response.sendRedirect().
If it's not, you'll need to manually copy it to response output stream:
Add the below code to your code.
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(my_file);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.flush();
You'll need to handle the appropriate exceptions, of course.
I added only these lines of code and it worked for me.
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
String mimeType =
URLConnection.guessContentTypeFromStream(inputStream);
if(mimeType==null) {
mimeType = "application/octet-stream";
}
response.setContentType(mimeType);
response.setContentLength((int)file.length());
response.setHeader("Content-Disposition",String.format("attachment; fileName=\"%s\"", file.getName()));
FileCopyUtils.copy(inputStream, response.getOutputStream());
Related
I have a site that can upload three pictures, that page saves some other data in the table in SQL Server, but it saves the name of one the pictures the same for pictures in database.
Of course, the name of the pictures are different in the folder that save.
My code:
foreach (var item in FileUpload)
{
if (item != null)
{
Random rnd = new Random();
string Pic = rnd.Next().ToString() + ".jpg";
// string Pic = System.IO.Path.GetFileName(file.FileName);
string Path = System.IO.Path.Combine(Server.MapPath("~/images/cover/"));
item.SaveAs(Path + Pic);
using (MemoryStream ms = new MemoryStream())
{
item.InputStream.CopyTo(ms);
byte[] array = ms.GetBuffer();
}
lstName.Add(Pic);
}
}
else
{
lstName.Add("9.jpg");
}
RContact.InsertContact(t, lstNam);
ViewBag.Style = "color:green;";
}
and I write in the Repository:
public bool InsertContact(tbl_contact t, List<string> PicsName)
{
db.tbl_contact.Add(t);
foreach (var item in PicsName)
{
t.picname = PicsName[0];
t.picnamet = PicsName[1];
t.picnametr = PicsName[2];
db.tbl_contact.Add(t);
}
return Convert.ToBoolean(db.SaveChanges());
}
but it save the same name.
I'm currently working on a project that requires importing the image drawn on a WPF Canvas (Mostly Geometries like lines and ellipses inside Path) into a Word Document. I'm using Openxml to do the job. Here's what I did
I convert read the content of the Canvas into a memory stream
I used the guide provided my Microsoft on how to insert image into Word files How to: Insert a picture into a word processing document (Open XML SDK)
Here's what I've got so far,
Canvas is converted into memory stream by the following piece of code
public static Stream CreateJPGStream(this Canvas canvas)
{
RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)canvas.ActualWidth, (int)canvas.ActualHeight, 96d, 96d, PixelFormats.Pbgra32);
canvas.Measure(new Size((int)canvas.ActualWidth, (int)canvas.ActualHeight));
canvas.Arrange(new Rect(new Size((int)canvas.ActualWidth, (int)canvas.ActualHeight)));
renderBitmap.Render(canvas);
var stream = new MemoryStream();
var encoder = new JpgBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
encoder.Save(stream);
return stream;
}
I believe this was not the cause of the problem since I have been using this code for a while now and I haven't had any issues so far
Then I just modified code from Microsoft Guide
public static void InsertPicture(this WordprocessingDocument word, Stream stream)
{
MainDocumentPart mainPart = word.MainDocumentPart;
if (mainPart == null)
{
mainPart = word.AddMainDocumentPart();
mainPart.Document = new Document() { Body = new Body() };
}
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
imagePart.FeedData(stream);
AddImageToBody(word, mainPart.GetIdOfPart(imagePart));
}
private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
{
var element =
new Drawing(
new DW.Inline(
new DW.Extent() { Cx = 990000L, Cy = 792000L },
new DW.EffectExtent()
{
LeftEdge = 0L,
TopEdge = 0L,
RightEdge = 0L,
BottomEdge = 0L
},
new DW.DocProperties()
{
Id = 1U,
Name = "Picture 1"
},
new DW.NonVisualGraphicFrameDrawingProperties(
new A.GraphicFrameLocks() { NoChangeAspect = true }),
new A.Graphic(
new A.GraphicData(
new PIC.Picture(
new PIC.NonVisualPictureProperties(
new PIC.NonVisualDrawingProperties()
{
Id = (UInt32Value)0U,
Name = "New Bitmap Image.jpg"
},
new PIC.NonVisualPictureDrawingProperties()),
new PIC.BlipFill(
new A.Blip(
new A.BlipExtensionList(
new A.BlipExtension()
{
Uri =
"{28A0092B-C50C-407E-A947-70E740481C1C}"
})
)
{
Embed = relationshipId,
CompressionState =
A.BlipCompressionValues.Print
},
new A.Stretch(
new A.FillRectangle())),
new PIC.ShapeProperties(
new A.Transform2D(
new A.Offset() { X = 0L, Y = 0L },
new A.Extents() { Cx = 990000L, Cy = 792000L }),
new A.PresetGeometry(
new A.AdjustValueList()
)
{ Preset = A.ShapeTypeValues.Rectangle }))
)
{ Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
)
{
DistanceFromTop = 0U,
DistanceFromBottom = 0U,
DistanceFromLeft = 0U,
DistanceFromRight = 0U,
EditId = "50D07946"
});
wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));
}
Now to join this two methods together
public void InsertPicture(Canvas c)
{
using (var word = OpenXmlHelper.Create(filePath))
{
var stream = c.CreateJPGStream();
word.InsertPicture(stream);
}
}
But when I open the document, I get this
Am I doing something wrong with the memorysteam or on the openxml side? Can someone enlighten me please.
PS: I have looked at similar questions already like Inserting Image into DocX using OpenXML and setting the size
After writing to a stream, the stream's position will be at the end of the stream. You must rewind the stream to the beginning before you read from it.
stream.Position = 0;
or
stream.Seek(0, System.IO.SeekOrigin.Begin);
I am trying from a .net client to download a file via a .net server (file is located on server machine ) using the StreamContent.However when launching the request i am getting the exception:
Exception
Stream does not support reading.
Client
class Program {
static async Task Main(string[] args) {
HttpClient client = new HttpClient();
using (FileStream stream = new FileStream("txt.path", FileMode.OpenOrCreate, FileAccess.Write)) {
var content = new StreamContent(stream);
var response = await client.PostAsync("http://localhost:5300/get", content);
}
}
}
Server
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
string fname = "dld.txt";
app.Run(async (context) => {
if (!(context.Request.Path == "get")) {
return;
}
File.WriteAllText(fname, "data is:" + DateTime.Now.ToString());
FileStream fs = new FileStream(fname, FileMode.Open, FileAccess.Read);
using (Stream stream = context.Response.Body) {
await fs.CopyToAsync(stream);
}
});
}
Hi you can use like this:
HttpContent stringContent = new StringContent(paramString); //if you want to use string
HttpContent fileStreamContent = new StreamContent(paramFileStream); //if you want to use file stream
HttpContent bytesContent = new ByteArrayContent(paramFileBytes);// if you want to use aray of bytes
using (var client = new HttpClient())
{
using (var formData = new MultipartFormDataContent())
{
formData.Add(stringContent, "param", "param");
formData.Add(fileStreamContent, "file", "file");
formData.Add(bytesContent, "file", "file");
var response = await client.PostAsync("some URL", formData);
if (!response.IsSuccessStatusCode)
{
return null;
}
return await response.Content.ReadAsStreamAsync();
}
}
I was having trouble getting the file because i wanted to use the Request.Body stream as a sink.I wanted the server to write the data on this stream (i thought the Request stream can be used both ways).
I have solved it by using the Response stream instead:
Client
static async Task Main(string[] args) {
HttpClient client = new HttpClient();
using (FileStream stream = new FileStream("data.txt", FileMode.OpenOrCreate, FileAccess.Write)) {
var content = new StringContent("not important");
var response = await client.PostAsync("http://localhost:5300/get",content);
await response.Content.CopyToAsync(stream);
}
}
I have hundreds of different random URLs coming in, all documents in libs, without any other parameters from different farms and different site collections and sites, goal is to download a file as a binary array from SharePoint.
So e.g. incoming url = http://a.b.c.d.e/f.g/h.i/j/k/l/m.docx .
So how to get the (a) correct site collection root url (b) site root url (c) library root url from this? The only way I now think of is slowly stripping off each part of the url until e.g. .Rootfolder no longer gives an exception... or the other way around slowly adding bits by the first part of the url until rootfolder nog longers gives an exception then query for subwebs etc..
The point is that ClientContext constructor accepts the url of web/site only.
But if the url will be specified in the following format:
http://site/web/documents/file.docx
then the exception System.Net.WebException will occur.
The following example demonstrates how to resolve ClientContext from request Url:
public static class ClientContextUtilities
{
/// <summary>
/// Resolve client context
/// </summary>
/// <param name="requestUri"></param>
/// <param name="context"></param>
/// <param name="credentials"></param>
/// <returns></returns>
public static bool TryResolveClientContext(Uri requestUri, out ClientContext context, ICredentials credentials)
{
context = null;
var baseUrl = requestUri.GetLeftPart(UriPartial.Authority);
for (int i = requestUri.Segments.Length; i >= 0; i--)
{
var path = string.Join(string.Empty, requestUri.Segments.Take(i));
string url = string.Format("{0}{1}", baseUrl, path);
try
{
context = new ClientContext(url);
if (credentials != null)
context.Credentials = credentials;
context.ExecuteQuery();
return true;
}
catch (Exception ex) {}
}
return false;
}
}
Usage
ClientContext context;
if (ClientContextUtilities.TryResolveClientContext(requestUri, out context, null))
{
using (context)
{
var baseUrl = requestUri.GetLeftPart(UriPartial.Authority);
var fileServerRelativeUrl = requestUri.ToString().Replace(baseUrl, string.Empty);
var file = context.Web.GetFileByServerRelativeUrl(fileServerRelativeUrl);
context.Load(file);
context.Load(context.Web);
context.Load(context.Site);
context.ExecuteQuery();
}
}
Since your goal is to download a file, there is pretty straightforward way to accomplish it without parsing url parts.
For example, using WebClient.DownloadFile Method:
private static void DownloadFile(Uri fileUri, ICredentials credentials, string localFileName)
{
using(var client = new WebClient())
{
client.Credentials = credentials;
client.DownloadFile(fileUri, localFileName);
}
}
I have made a working method but it seems elaborate, so any suggestions for improvement are welcome just to "download file if one of the specific columns has value "yes":
public void getDocument(Document doc)
{
// get the filename
Uri uri = new Uri(doc.uri);
doc.filename = "";
doc.filename = System.IO.Path.GetFileName(uri.LocalPath);
//string fullPathWithoutFileName = docUri.Replace(filename, "");
// would also include ?a&b so:
string[] splitDocUri = doc.uri.Split('/');
string fullPathWithoutFileName = "";
for (int i = 0; i < splitDocUri.Length -1; i++)
{
fullPathWithoutFileName += (splitDocUri[i] + '/');
}
// get via "_api/contextinfo" the context info
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(fullPathWithoutFileName + "_api/contextinfo");
req.Method = "POST";
req.Accept = "application/json; odata=verbose";
req.Credentials = new NetworkCredential(doc.username, doc.password, doc.domain);
req.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED","f");
req.ContentLength = 0;
BypassCertificateError();
HttpWebResponse rp = (HttpWebResponse)req.GetResponse();
Stream postStream = rp.GetResponseStream();
StreamReader postReader = new StreamReader(postStream);
string results = postReader.ReadToEnd();
// Now parse out some values needs system.web.extensions
JavaScriptSerializer jss = new JavaScriptSerializer();
var d = jss.Deserialize<dynamic>(results);
string formDigestValue = d["d"]["GetContextWebInformation"]["FormDigestValue"];
// the full url to the website e.g. "http://server:7777/level1/level 2"
string webFullUrl = d["d"]["GetContextWebInformation"]["WebFullUrl"];
// the full url to the site collection e.g. "http://server:7777"
string siteFullUrl = d["d"]["GetContextWebInformation"]["SiteFullUrl"];
// now we can create a context
ClientContext ctx = new ClientContext(webFullUrl);
ctx.ExecutingWebRequest +=
new EventHandler<WebRequestEventArgs>(ctx_MixedAuthRequest);
BypassCertificateError();
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
ctx.Credentials = new NetworkCredential(doc.username, doc.password, doc.domain);
// Get the List
Microsoft.SharePoint.Client.File file = ctx.Web.GetFileByServerRelativeUrl(uri.AbsolutePath);
List list = file.ListItemAllFields.ParentList;
ctx.Load(list);
ctx.ExecuteQuery();
// execute a CAML query against it
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
"<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/>" +
"<Value Type='Text'>" + doc.filename + "</Value></Eq></Where>" +
"<RowLimit>1</RowLimit></Query></View>";
ListItemCollection listItems = list.GetItems(camlQuery);
ctx.Load(listItems);
try {
ctx.ExecuteQuery();
}
catch
{
// e.g. : no access or the listname as incorrectly deduced
throw;
}
// and now retrieve the items needed
if (listItems.Count == 1)
{
ListItem item = listItems[0];
// some more checking from testColumn to decide if to download yes/no
string testColumn;
if (item.IsPropertyAvailable("testColumn")) {
testColumn = (string)item["testColumn"];
}
FileInformation fileInformation =
Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx,
(string)item["FileRef"]);
doc.bytes = ReadFully(fileInformation.Stream);
}
else
{
doc.errormessage = "Error: No document found";
}
}
I have been trying to send a image to restful service and some data with it. But i can send data (Name and Description of image) and also i created sql database to store data and data is added on it but i can't send image to the server.
the code for service:
[WebInvoke(UriTemplate = "UploadPhoto/{fileName}/{description}", Method = "POST")]
public void UploadPhoto(string fileName, string description, Stream fileContents)
{
byte[] buffer = new byte[32768];
MemoryStream ms = new MemoryStream();
int bytesRead, totalBytesRead = 0;
do
{
bytesRead = fileContents.Read(buffer, 0, buffer.Length);
totalBytesRead += bytesRead;
ms.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
// Save the photo on database.
using (DataAcess data = new DataAcess())
{
var photo = new Photo() { Name = fileName, Description = description, Data = ms.ToArray(), DateTime = DateTime.UtcNow, };
data.InsertPhoto(photo);
}
ms.Close();
Console.WriteLine("Uploaded file {0} with {1} bytes", fileName, totalBytesRead);
}
And this is my code on client side. I am doing it on windows phone 7.
void btnNewPhoto_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("http://localhost:2557/photos");
string requestUrl = string.Format("{0}/UploadPhoto/{1}/{2}", uri, System.IO.Path.GetFileName(txtFileName.Text), txtDescription.Text);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
request.Method = "POST";
request.BeginGetRequestStream
(result =>
{
// Sending the request.
using (var requestStream = request.EndGetRequestStream(result))
{
using (StreamWriter writer = new StreamWriter(requestStream))
{
BinaryReader reader = new BinaryReader(requestStream);
string s = imgPhoto.ToString();
byte[] byteArray = Encoding.UTF8.GetBytes(s.ToString());
requestStream.Write(byteArray, 0, byteArray.Length);
requestStream.Close();
requestStream.Dispose();
//writer.Write(requestUrl);
//writer.Flush();
}
}
// Getting the response.
request.BeginGetResponse(responseResult =>
{
var webResponse = request.EndGetResponse(responseResult);
using (var responseStream = webResponse.GetResponseStream())
{
using (var streamReader = new StreamReader(responseStream))
{
string srresult = streamReader.ReadToEnd();
}
}
}, null);
}, null);
}