Set view content directly instead of using a template - zend-framework2

I was wondering if anyone knew of a way to set the content of a view without using a template in Zend Framework 2. I have users creating emails in ckeditor, storing in text files, then grabbing the file contents and trying to produce a pdf for printing. Here's what is going on in the controller:
$emails = $this -> email() -> getEmails();
//Render the display
$viewRender = $this->getServiceLocator()->get('ViewRenderer');
$view->setTemplate('module/controller/template.phtml');
$html = $viewRender->render($view);
//Create the HTML to PDF class instance & set the bin path
$wkpdf = new WkHtmlToPdf(array('bin'=>'C:\\Program Files (x86)\\wkhtmltopdf\\wkhtmltopdf'));
//Add it to the PDF
$wkpdf->addPage($html);
//Send it to the client
$wkpdf->send();
I want to replace the setTemplate() method with something that will directly set the view's content with the html string I have retrieved from the email files. Any thoughts?
This is what I came up with:
$emails = $this -> email() -> getEmails();
$email = $emails->$email_name;
//Create the HTML to PDF class instance & set the bin path
$wkpdf = new \WkHtmlToPdf(array('bin'=>$this->settings()->getSettings()->wkhtmltopdf_path));
//Add the email to the PDF
$wkpdf->addPage("<html>".$email."</html>");
//Send it to the client
$wkpdf->send();
All I needed to do was wrap the string with html tags for WkHtmlToPdf to process it.

In your action, before returning content to your view file,
$result = new ViewModel(array('view_contant_variable' => $content_to_assign));
$result->setTerminal(true);
return $result;
I hope this, may help to you to display your view file without template contents.

Related

How to store the text in a string variable from a text file in the asset directory?

I am doing some test with xamarin android. I have a text file in the asset folder, that I know how to access to this file:
using (Stream myFile= Assets.Open("myFile.txt"))
{
using (FileStream myDestinationFile= File.Create("myDestinationPath"))
{
myFile.CopyTo(myDestinationFile);
}
}
With this code, I can copy the file to another location, for example, "Personal", so I can edit the file because I know that I can't edit the files in the assets folder.
But I would like to pass the information of the text file directly to a string variable, beacause I don't need to edit the file, just to access to the information and pass this string as parameter of a method.
Is it possible to set a string variable with the information of the text file of the asset folder?
Thanks.
But I would like to pass the information of the text file directly to a string variable, beacause I don't need to edit the file, just to access to the information and pass this string as parameter of a method.
If you want to pass the string from the text file in Assect folder, you could use AssetManager.
Text file:
Set the Build Action to AndroidAssect:
Code:
// Create a new TextView and set it as our view
TextView tv = new TextView(this);
// Read the contents of our asset
string content;
AssetManager assets = this.Assets;
using (StreamReader sr = new StreamReader(assets.Open("TextFile1.txt")))
{
content = sr.ReadToEnd();
}
// Set TextView.Text to our asset content
tv.Text = content;
SetContentView(tv);
Result:

pdf.js to display output of file created with tcpdf

I really hope you will be able to help me out on this one.
I am new to pdf.js so for the moment, I am playing around with the pre-built version to see if I can integrate this into my web app.
My problem:
I am using tcpdf to generate a pdf file which I would like to visualize using pdf.js without having to save it to a file on the server.
I have a php file (generate_document.php) that I use to generate the pdf. The file ends with the following:
$pdf->Output('test.pdf', 'I');
according to the tcpdf documentation, the second parameter can be used to generate the following formats:
I: send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.
D: send to the browser and force a file download with the name given by name.
F: save to a local server file with the name given by name.
S: return the document as a string (name is ignored).
FI: equivalent to F + I option
FD: equivalent to F + D option
E: return the document as base64 mime multi-part email attachment (RFC 2045)
Then, I would like to view the pdf using pdf.js without creating a file on the server (= not using 'F' as a second parameter and passing the file name to pdf.js).
So, I thought I could simply create an iframe and call the pdf.js viewer pointing to the php file:
<iframe width="100%" height="100%" src="/pdf.js_folder/web/viewer.html?file=get_document.php"></iframe>
However, this is not working at all....do you have any idea what I am overlooking? Or is this option not available in pdf.js?
I have done some research and I have seen some posts here on converting a base64 stream to a typed array but I do not see how this would be a solution to this problem.
Many thanks for your help!!!
EDIT
#async, thanks for your anwer.
I got it figured out in the meantime, so I thought I'd share my solution with you guys.
1) In my get_document.php, I changed the output statement to convert it directly to base64 using
$pdf_output = base64_encode($pdf->Output('test_file.pdf', 'S'));
2) In viewer.js, I use an XHR to call the get_document.php and put the return in a variable (pdf_from_XHR)
3) Next, I convert what came in from the XHR request using the solution that was already mentioned in a few other posts (e.g. Pdf.js and viewer.js. Pass a stream or blob to the viewer)
pdf_converted = convertDataURIToBinary(pdf_from_XHR)
function convertDataURIToBinary(dataURI) {
var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
var base64 = dataURI.substring(base64Index);
var raw = window.atob(base64);
var rawLength = raw.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));
for (i = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i);
}
return array;
}
et voilĂ  ;-)
Now i can inject what is coming from that function into the getDocument statement:
PDFJS.getDocument(pdf_converted).then(function (pdf) {
pdfDocument = pdf;
var url = URL.createObjectURL(blob);
PDFView.load(pdfDocument, 1.5)
})

Web server to save a file, then open it and save as different type, then prompt user to download

I have an MVC Razor application where I am returning a view.
I have overloaded my action to accept a null-able "export" bool which will change the action by adding headers but still returning the same view as a file (in a new window).
//if there is a value passed in, set the bool to true
if (export.HasValue)
{
ViewBag.Exporting = true;
var UniqueFileName = string.Format(#"{0}.xls", Guid.NewGuid());
Response.AddHeader("content-disposition", "attachment; filename="+UniqueFileName);
Response.ContentType = "application/ms-excel";
}
As the file was generated based on a view, its not an .xls file so when opening it, I get the message "the file format and extension of don't match". So after a Google, I have found THIS POST on SO where one of the answers uses VBA to open the file on the server (which includes the HTML mark-up) then saves it again (as .xls).
I am hoping to do the same, call the controller action which will call the view and create the .xls file on the server, then have the server open it, save it then return it as a download.
What I don't want to do is to create a new view to return a clean file as the current view is an extremely complex page with a lot of logic which would only need to be repeated (and maintained).
What I have done in the view is to wrap everything except the table in an if statement so that only the table is exported and not the rest of the page layout.
Is this possible?
You can implement the VBA in .net
private void ConvertToExcel(string srcPath, string outputPath, XlFileFormat format)
{
if (srcPath== null) { throw new ArgumentNullException("srcPath"); }
if (outputPath== null) { throw new ArgumentNullException("outputPath"); }
var excelApp = new Application();
try
{
var wb = excelApp.Workbooks.Open(srcPath);
try
{
wb.SaveAs(outputPath, format);
}
finally
{
Marshal.ReleaseComObject(wb);
}
}
finally
{
excelApp.Quit();
}
}
You must install Microsoft.Office.Interop and add reference to a COM oject named Microsoft Excel XX.0 Object Library
Sample usage:
//generate excel file from the HTML output of GenerateHtml action.
var generateHtmlUri = new Uri(this.Request.Url, Url.Action("GenerateHtml"));
ConvertToExcel(generateHtmlUri.AbsoluteUri, #"D:\output.xlsx", XlFileFormat.xlOpenXMLStrictWorkbook);
I however discourage this solution because:
You have to install MS Excel in your web server.
MS Excel may sometimes misbehave like prompting a dialog box.
You must find a way to delete the generated Excel file afterwards.
Ugly design.
I suggest to generate excel directly because there doesn't seem to be better ways to covert HTML to Excel except using Excel itself or DocRaptor.

Create and download word file from template in MVC

I have kept a word document (.docx) in one of the project folders which I want to use as a template.
This template contains custom header and footer lines for user. I want to facilitate user to download his own data in word format. For this, I want to write a function which will accept user data and referring the template it will create a new word file replacing the place-holders in the template and then return the new file for download (without saving it to server). That means the template needs to be intact as template.
Following is what I am trying. I was able to replace the placeholder. However, I am not aware of how to give the created content as downloadable file to user. I do not want to save the new content again in the server as another word file.
public void GenerateWord(string userData)
{
string templateDoc = HttpContext.Current.Server.MapPath("~/App_Data/Template.docx");
// Open the new Package
Package pkg = Package.Open(templateDoc, FileMode.Open, FileAccess.ReadWrite);
// Specify the URI of the part to be read
Uri uri = new Uri("/word/document.xml", UriKind.Relative);
PackagePart part = pkg.GetPart(uri);
XmlDocument xmlMainXMLDoc = new XmlDocument();
xmlMainXMLDoc.Load(part.GetStream(FileMode.Open, FileAccess.Read));
xmlMainXMLDoc.InnerXml = ReplacePlaceHoldersInTemplate(userData, xmlMainXMLDoc.InnerXml);
// Open the stream to write document
StreamWriter partWrt = new StreamWriter(part.GetStream(FileMode.Open, FileAccess.Write));
xmlMainXMLDoc.Save(partWrt);
partWrt.Flush();
partWrt.Close();
pkg.Close();
}
private string ReplacePlaceHoldersInTemplate(string toReplace, string templateBody)
{
templateBody = templateBody.Replace("#myPlaceHolder#", toReplace);
return templateBody;
}
I believe that the below line is saving the contents in the template file itself, which I don't want.
xmlMainXMLDoc.Save(partWrt);
How should I modify this code which can return the new content as downloadable word file to user?
I found the solution Here!
This code allows me to read the template file and modify it as I want and then to send response as downloadable attachment.

How to set the url in IHTMLDocument2 without it navigating to a new page

How do I set the url of an htmldocument after I've written to it. For example:
WebBrowser wb = new WebBrowser();
wb.Navigate(new Uri(location, UriKind.Absolute));
IHTMLDocument2 myDoc = new HTMLDocumentClass();
myDoc.write(new object[] { wb.DocumentText});
myDoc.close();
If I do myDoc.url = "http://www.google.com" it attempts to load google.
How do I set the url without having it attempt to load that url?
These steps should give you a document with correct URL and your own content:
Create document directly from URL (so you don't have to set the URL later)
Stop document download (because you don't need the content)
Fill document with your content
This code shows how to do it:
// 1. Create new document from URL
IHTMLDocument2 NewDoc = (wb.Document as IHTMLDocument4).createDocumentFromUrl("http://www.stackoverflow.com", "null");
// 2. Immediately stop navigating; the URL is still set
NewDoc.execCommand("Stop", false, null);
// 3. Now write your stuff to the document
// ...
Note: It's hard to guess how much content can be downloaded between steps 1 and 2 because loading happens asynchronously. So it's probably good to check if the document is indeed empty before doing step 3. If not, clear the document and proceed.

Resources