I am trying to convert pdf stream back to pdf file, then save it on my server.
The function GetHTTPRequest gets pdf url and returns the pdf url stream string.
I need to convert this stream to pdf file.
My Code:
public ActionResult Html(string strUrl)
{
string xhr;
xhr = GetHTTPRequest(strUrl, "GET");
// make pdf file fron xhr
return View("Index");
}
[ChildActionOnly]
public static string GetHTTPRequest(string RequestUrl, string RequestMethod)
{
try
{
HttpWebRequest r = (HttpWebRequest)WebRequest.Create(RequestUrl);
r.Method = RequestMethod;
HttpWebResponse res = (HttpWebResponse)r.GetResponse();
Stream sr = res.GetResponseStream();
StreamReader sre = new StreamReader(sr);
string s = sre.ReadToEnd();
return s;
}
catch (Exception)
{
return string.Empty;
}
}
This is only 1 line from what xhr contains:
"%PDF-1.3\n%�쏢\n5 0 obj\n<>\nstream\nx��\k���V �DB��rq��\r}��_�4U[��\nTҮU�~�ِBI h����>gl����n�N���Y{.�~�33�;�h����뷶.��W7�n����ͭ;[������[կ��?TR��T�_l�6� �O�e�ll��\m\b�����T����F�����V��n���6h嫍��Hk��l�R��O6ô3�7O5�:�.�i<�:���3��S�j��V�O��\b�~&\r�S93�l�ѭ�Q�z�v�cF)���\r��g��m�v����Z���\a���;{�i��Bq�7��^xK�7�U��P?��z>����]���F\b1�X�ec��/4�ji��/К��&#�EjF)�g�D�\v)��_ �\r|��XSEcu�\nILz��H|�ZL�a�wO\af^m6�NzS_lb$�Vx\rB�VG��_���44�v���������w������4J����Q�z���7�刭��+�a�|�7�Z����&��ٕQ��LsE��c�t뜁������)�ad���ӷ2��inSU��-��\a��\fā4ʹ�v1�w�ֽ����)�����S0�����2M�~�S�;�Ԩ2�\a|�����'K�0[\"{��F��5��2�ٱJ[��ӑ ��F�����3��X�s'\b9�[�&�Et��A\"�����N���������)7=�p�T�v.�!�q\"��H����M-i�,��TA��P����tV{]V=z����\rV����T��o�c�s�������[w*i�g��R�EZ�Λ.JQ}�\r:�w)�Ȕڣ�����{�)e%�(GF3�H8�G�Ԣ5)G^u�C�F�ÂQ\v�A=I\"�G\"�#�i�X�3d�����P;%=r���8#\he<�IAv��\a������D[ؕ]���7X��!��\b#B�Y$a5\b \b��T�.���Կ,\n�)1�V�\0N�z�$Pȡ����%
Can someone please put some light on it and solve it for me?
I never used stream libary before.
Thanks.
PDF is a binary file format. Don't use a string it doesn't make sense. What you are doing is eqivalent to trying to open a pdf file with NotePad. You should save the pdf file as file.
This is how you would download the file.
string resourceUrl = "http://www.google.com/file.pdf";
string fileName = downloadedFile.pdf;
WebClient myWebClient = new WebClient();
myWebClient.DownloadFile(resourceUrl,fileName);
If you need to read file.pdf as a string that's a different matter. You will need to look for a pdf parser. iTextSharp is not a bad choice for a start.
Related
I need to convert any file coming from web response into .pdf format, I'm currently getting it word docx file format from the URL and saving it into memory stream so i can later insert it in it's designated library.
The problem I'm facing now is that I'm saving my docx files directly into .pdf by putting an extension at the end which obviously ends up not opening the file later, So i'm trying to convert my memory stream into pdf directly .
Here is my piece of code that i tried to convert the the stream to .pdf but it looks like the file isn't getting converted correctly.
private Stream DownloadFromUrl(string url)
{
var webRequest = WebRequest.Create(url);
webRequest.Credentials = CredentialCache.DefaultNetworkCredentials;
webRequest.PreAuthenticate = true;
webRequest.UseDefaultCredentials = true;
//EventLogUtility.LogInformationMessage(DocumentURL);
string message = string.Empty;
using (Stream outputStream = new MemoryStream())
{
using (var response = webRequest.GetResponse())
{
using (var content = response.GetResponseStream())
{
var memory = new MemoryStream();
content.CopyTo(memory);
Document doc = new Document(memory);
doc.Save(memory, SaveFormat.Pdf);
return memory;
}
}
}
}
If the content in the stream is actually in the Microsoft Word file format (and not just plain text), then you need to map the format to the file format for PDF. I know there is a 'Print to PDF' function available in Word, you could try looking into that.
I'm trying to create a scanning solution. Basically the user is physically scanning a page. The printer is making an API call, passing in the binary data of the scan in the body.
I'm trying to save this as a PDF on the server, but when I go to open the file, i'm getting an error "There is an error while reading a stream".
var bodyStream = new StreamReader(HttpContext.Current.Request.InputStream);
bodyStream.BaseStream.Seek(0, SeekOrigin.Begin);
var bodyText = bodyStream.ReadToEnd();
string pathToFiles = HttpContext.Current.Server.MapPath("~\\UploadedFiles\\WriteLines.pdf");
try
{
using (StreamWriter outputFile = new StreamWriter(pathToFiles, false))
{
outputFile.WriteLine(bodyText);
}
HttpContext.Current.Response.ContentType = "application/pdf";
}
catch (Exception ex)
{
throw (ex);
}
This is just testing something, and I have permissions etc for writing the file, it's just not creating a valid file.
Any thoughts on what I should use? I have looked into some libraries, but they don't seem to cover what i'm after
StreamReader.ReadToEnd convert bytes to string in particular encoding (UTF8 by default). I don't think this work for PDF.
You need copy bytes directly in the output file :
var bodyStream = HttpContext.Current.Request.InputStream;
bodyStream.Seek(0, SeekOrigin.Begin);
string pathToFiles = HttpContext.Current.Server.MapPath("~\\UploadedFiles\\WriteLines.pdf");
using (FileStream outputFile = File.Create(pathToFiles))
{
bodyStream.CopyTo(outputFile);
}
Im trying to create a web service which gets to a URL e.g. www.domain.co.uk/prices.csv and then reads the csv file. Is this possible and how? Ideally without downloading the csv file?
You could use:
public string GetCSV(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string results = sr.ReadToEnd();
sr.Close();
return results;
}
And then to split it:
public static void SplitCSV()
{
List<string> splitted = new List<string>();
string fileList = getCSV("http://www.google.com");
string[] tempStr;
tempStr = fileList.Split(',');
foreach (string item in tempStr)
{
if (!string.IsNullOrWhiteSpace(item))
{
splitted.Add(item);
}
}
}
Though there are plenty of CSV parsers out there and i would advise against rolling your own. FileHelpers is a good one.
// Download the file to a specified path. Using the WebClient class we can download
// files directly from a provided url, like in this case.
System.Net.WebClient client = new WebClient();
client.DownloadFile(url, csvPath);
Where the url is your site with the csv file and the csvPath is where you want the actual file to go.
In your Web Service you could use the WebClient class to download the file, something like this ( I have not put any exception handling, not any using or Close/Dispose calls, just wanted to give the idea you can use and refine/improve... )
using System.Net;
WebClient webClient = new WebClient();
webClient.DownloadFile("http://www.domain.co.uk/prices.csv");
then you can do anything you like with it once the file content is available in the execution flow of your service.
if you have to return it to the client as return value of the web service call you can either return a DataSet or any other data structure you prefer.
Sebastien Lorion's CSV Reader has a constructor that takes a Stream.
If you decided to use this, your example would become:
void GetCSVFromRemoteUrl(string url)
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
using (CsvReader csvReader = new CsvReader(response.GetResponseStream(), true))
{
int fieldCount = csvReader.FieldCount;
string[] headers = csvReader.GetFieldHeaders();
while (csvReader.ReadNextRecord())
{
//Do work with CSV file data here
}
}
}
The ever popular FileHelpers also allows you to read directly from a stream.
The documentation for WebRequest has an example that uses streams. Using a stream allows you to parse the document without storing it all in memory
I want to send a url as query string e.g.
localhost/abc.aspx?url=http:/ /www.site.com/report.pdf
and detect if the above URL returns the PDF file. If it will return PDF then it gets saved automatically otherwise it gives error.
There are some pages that uses Handler to fetch the files so in that case also I want to detect and download the same.
localhost/abc.aspx?url=http:/ /www.site.com/page.aspx?fileId=223344
The above may return a pdf file.
What is best way to capture this?
Thanks
You can download a PDF like this
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse response = req.GetResponse();
//check the filetype returned
string contentType = response.ContentType;
if(contentType!=null)
{
splitString = contentType.Split(';');
fileType = splitString[0];
}
//see if its PDF
if(fileType!=null && fileType=="application/pdf"){
Stream stream = response.GetResponseStream();
//save it
using(FileStream fileStream = File.Create(fileFullPath)){
// Initialize the bytes array with the stream length and then fill it with data
byte[] bytesInStream = new byte[stream.Length];
stream.Read(bytesInStream, 0, bytesInStream.Length);
// Use write method to write to the file specified above
fileStream.Write(bytesInStream, 0, bytesInStream.Length);
}
}
response.Close();
The fact that it may come from an .aspx handler doesn't actually matter, it's the mime returned in the server response that is used.
If you are getting a generic mime type, like application/octet-stream then you must use a more heuristical approach.
Assuming you cannot simply use the file extension (eg for .aspx), then you can copy the file to a MemoryStream first (see How to get a MemoryStream from a Stream in .NET?). Once you have a memory stream of the file, you can take a 'cheeky' peek at it (I say cheeky because it's not the correct way to parse a PDF file)
I'm not an expert on PDF format, but I believe reading the first 5 chars with an ASCII reader will yield "%PDF-", so you can identify that with
bool isPDF;
using( StreamReader srAsciiFromStream = new StreamReader(memoryStream,
System.Text.Encoding.ASCII)){
isPDF = srAsciiFromStream.ReadLine().StartsWith("%PDF-");
}
//set the memory stream back to the start so you can save the file
memoryStream.Position = 0;
I generate the text file in my code using the stream writer using mvc format for web application . But my text file not download the my page? my sample code is below:
// file have the all values in stream writerformat
return File(file, "text/plain", "Export.txt");
try adding this before your return:
var cd = new System.Net.Mime.ContentDisposition
{
FileName = "Export.txt",
Inline = false,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(file, "text/plain");
You can call a method that will give you your file stream like this:
public FileStreamResult DownloadFile()
{
//
// This part is where you build your file
//
// file should be a Stream
return new FileStreamResult(file, "text/plain")
{
FileDownloadName = "optional_name_that_you_can_give_your_file"
};
}