Open local pdf file using pdf.js in firefox extension - firefox-addon

I am using pdf.js to extract annotations from pdf files in a firefox add-on. Everything works pretty well except opening local pdf files. PDFJS.getDocument('http://...').then(function(pdf) { ... works but PDFJS.getDocument('file:///...').then(function(pdf) { does not.
I am getting this error: Unexpected server response (0) while retrieving PDF ...
Is this a cross-domain issue? I thought that shouldn't be a problem because my code is running from an extension. How can I solve this problem?

Read the file into a Uint8Array and pass it as source to getDocument.

Here is a simplified JS that works for me as illustration only -
You first get the file as array buffer (e.target.result) then convert it to Uint8 array and pass it to PDF.js as data (docInitParams) instead of a URL
var reader = new FileReader();
reader.readAsArrayBuffer(input.files[0]);
reader.onload = function (e) {
var myData = new Uint8Array(e.target.result)
var docInitParams = { data: myData };
PDFJS.getDocument(docInitParams).then(function(pdf) {.....})
}

Related

How do I save edited PDF file from PDFtron?

I have used PDFTron to update/edit PDF files. I have followed the documentation for opening the PDF file which came from server, but I am not sure how to save the edited PDF file with this SDK (PDFTron).
I have referred below links to save PDF, but did not succeed.
https://www.pdftron.com/documentation/ios/guides/features/forms/export-data/
https://www.pdftron.com/api/ios/Enums/PTSaveOptions.html
I want to send XFDF file formats to server.
PDFTron saves PDF with annotation automatically after some time interval, but I want it to be saved by save button press. I am stuck on this saving process.
I have below code to import annotation and I don't know how to import this XFDF file and where do to get this XFDF file.
// Import annotations from XFDF to FDF
let fdf_doc: PTFDFDoc = PTFDFDoc.create(fromXFDF: xfdf_filename)
// Optionally read XFDF from a string
let fdf_doc: PTFDFDOc = PTFDFDoc.create(fromXFDF: xfdf_string)
// Merge FDF data into PDF doc
let doc: PTPDFDoc = PTPDFDoc(filepath: filename)
doc.fdfMerge(fdf_doc)
I don't want it to be customisations by myself, I just want it to be saved by me on pressing button.
Below is my query
How do I save the applied annotation on PDF by myself?
Once you've applied changes to the document data you'll probably want to do something with the updated PDF like letting the user download it or sending it back to your server.
If you just want to let the user download the edited file then no extra changes are necessary as pressing the download button will save the modified PDF to the user's computer.
To add a custom save button, here is a code sample.
If you want to get the modified PDF as an ArrayBuffer then you can use the
getFileData function on Document.
For example:
WebViewer(...)
.then(instance => {
const { documentViewer, annotationManager } = instance.Core;
documentViewer.addEventListener('documentLoaded', async () => {
const doc = documentViewer.getDocument();
const xfdfString = await annotationManager.exportAnnotations();
const options = { xfdfString };
const data = await doc.getFileData(options);
const arr = new Uint8Array(data);
const blob = new Blob([arr], { type: 'application/pdf' });
// upload blob to your server
});
});
I have followed the documentation for opening the PDF file which came from server
There are a few ways to do this - could you share which API you are using?
The main point of your question seems to be how to save the PDF via a button press (after you've merged in XFDF annotation data). Is this the case?
You can control where a remote document is shared by implementing the relevant delegate methods, likely specifically https://www.pdftron.com/api/ios/Protocols/PTDocumentControllerDelegate.html#/c:objc(pl)PTDocumentControllerDelegate(im)documentController:destinationURLForDocumentAtURL:
You can then save the document using this method:
https://www.pdftron.com/api/ios/Classes/PTDocumentBaseViewController.html#/c:objc(cs)PTDocumentBaseViewController(im)saveDocument:completionHandler:

Excel export returning two files in MVC

I am trying to export some data to excel and download the file via the browser. I have a method that creates an active workbook that returns a byte array in the following way:
byte[] doc = be.GetActiveWorkbook(excelApp);
The method GetActiveWorkBook looks like this:
public byte[] GetActiveWorkbook(Application app)
{
string path = Path.GetTempFileName();
try
{
app.ActiveWorkbook.SaveCopyAs(path);
return File.ReadAllBytes(path);
}
finally
{
if (File.Exists(path))
File.Delete(path);
}
}
Lastly, the file is returned like this:
var file = File(doc, "application/vnd.ms-excel");
file.FileDownloadName = filename + " " + id + ".xlsx";
return file;
The excel file is indeed downloaded to the browser, however it seems like there are processes in the background that are still active, even though I close the excel file in my desktop. Why is this?
The Excel Interop is a nightmare for disposing of properly. Probably worth checking your code outside of the method you've posted to see how you're cleaning up the Excel Application that's passed in:
How do I properly clean up Excel interop objects?

Security Error when trying to load ByteArray from mediaPromise

in a mobile application i need to send an image which the user either took with the camera or picked from a cameraroll.
I am using the starling framework and feathersUI ( although i think this does not matter to problem )
When the mediapromise is loaded using loadFilePromise i use the following code to deal with the image data:
_mediaLoader = new Loader()
//loading the filePromise from CameraRoll
_mediaLoader.loadFilePromise(_mediaPromise);
_mediaLoader.contentLoaderInfo.addEventListener(starling.events.Event.COMPLETE, onLoadImageComplete);
private function onLoadImageComplete(event:flash.events.Event=null):void {
//creating the starling texture to display the image inside the application
var texture:Texture = Texture.fromBitmapData(Bitmap(_mediaLoader.content).bitmapData, false, false, 1);
//now trying to load the content into a bytearray to send to the server later
var bytes:ByteArray=_mediaLoader.contentLoaderInfo.bytes;
}
the last line of code results in a Security error:
Error #2044: Unhandled SecurityErrorEvent:. text=Error #2121: Security sandbox violation: app:/myapp.swf: http://adobe.com/apollo/[[DYNAMIC]]/1 cannot access . This may be worked around by calling Security.allowDomain.
I tried
Security.allowDomain("*")
as a test
but then i get:
SecurityError: Error #3207: Application-sandbox content cannot access this feature.
As a workaround i write my own png ByteArray inside the Application from the loaders BitmapData using Adobes PNGEncoder Class:
var ba:ByteArray=PNGEncoder.encode(Bitmap(_mediaLoader.content).bitmapData)
But this takes a significant amount of time ...
I also tried the FileReference to load the image but
_mediaPromise.file
and
_mediaPromise.relativePath
are both null.
What am I doing wrong? Or is this a known problem ?
Thanks!
Hello I have found a solution based on a post about the processing of exif data mentioned here: http://blogs.adobe.com/cantrell/archives/2011/10/parsing-exif-data-from-images-on-mobile-devices.html
the crucial code
private function handleMedia(event:MediaEvent):void{
_mediaPromise=event.data as MediaPromise;
_imageBytes=new ByteArray()
var mediaDispatcher:IEventDispatcher = _mediaPromise.open() as IEventDispatcher;
mediaDispatcher.addEventListener(ProgressEvent.PROGRESS, onMediaPromiseProgress);
mediaDispatcher.addEventListener(flash.events.Event.COMPLETE, onMediaPromiseComplete);
};
private function onMediaPromiseProgress(e:ProgressEvent):void{
var input:IDataInput = e.target as IDataInput;
input.readBytes(_imageBytes, _imageBytes.length, input.bytesAvailable);
};
private function onMediaPromiseComplete(e:flash.events.Event):void{
_mediaLoader = new Loader();
_mediaLoader.loadBytes(_imageBytes)
};
works like a charm for me on ipad and iphone.

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)
})

How do I send file back after Kendo upload

I have the following requirements. I need to upload an Excel file to a MVC based site. For this I am using Kendo Upload. In the controller action that handles the upload I need to make a slight modification to the Excel file and then send it back as a file stream. I am using Aspose for the Excel modifications. My question is can I achieve all of this within the one controller action without the Excel file ever hitting the disk of web server?
I managed to get this to work by using the synchronous upload mode. My controller action looks like this:
[POST("SaveExcelFile")]
public FileStreamResult Save(IEnumerable<HttpPostedFileBase> files)
{
// The Name of the Upload component is "files"
if (files != null)
{
foreach (var file in files)
{
// Some browsers send file names with full path.
// We are only interested in the file name.
var fileName = Path.GetFileName(file.FileName);
//var physicalPath = Path.Combine(Server.MapPath("~/App_Data"), fileName);
Workbook excel2 = new Workbook(file.InputStream);
excel2.Worksheets.Add("TEST");
Stream stream = new MemoryStream();
excel2.Save(stream, SaveFormat.Excel97To2003);
stream.Position = 0;
return File(stream, "application/vnd.ms-excel", "junk.xls");
// The files are not actually saved in this demo
// file.SaveAs(physicalPath);
}
}
// Return an empty string to signify success
return null;
}
This is only proof of concept code but you can get the idea of what I was trying to achieve. Upload a file, manipulate it and send the modified Worksheet back down to the client as a stream.
I don't think you can. I have used KendoUI's upload control, and it seems that you'll only get to manipulate the file after it's written on the server side.
What you can do is to first save the file, perform your modification, then overwrite it.

Resources