Desktop version

Main > Forum > 1C:Enterprise Platform > 1C:Enterprise – Business applications platform > PDF Attachment

Forum

Search UsersRules
PDF Attachment
#1
Active user
Points:: 0
Joined:: Jun 4, 2013

Hi, I'm wondering if I can attache a PDF file into a documnet or catalog record.

And thanks for being helpful in advance

Profile
#2
Guest
Points::
Joined::

Yes, you need to create an attribute of ValueStorage type and load your file into it.

Profile
#3
Active user
Points:: 0
Joined:: Jun 4, 2013

Hi, I did what you've told me ... i think it's working but i can't find a way to open it, I'll show you my code.

here the user can choose the pdf from a browser  

Code
&AtClient
Procedure Command1(Command)
                   
    Dialog = New FileDialog(FileDialogMode.Open);
    If Dialog.Choose() Then
        pdf= ss(Dialog.FullFileName);
    EndIf;

EndProcedure

&AtServer
function ss(dialog)

    pdf= new ValueStorage (dialog,New Deflation(9));
    Return pdf      

EndFunction

Do you have any suggestion or recommendation?

Profile
#4
Guest
Points::
Joined::

You were trying to put a file name into the value storage. Please see How to store a file in the database? topic for example.

Profile
#5
Active user
Points:: 0
Joined:: Jun 4, 2013

Ok i think i did it right ... look at my code now:

Code
&AtClient
Procedure LoadFile()
   StorageName = "";
   
    FileName ="C:\Users\mohammad.maleh\Desktop\1C.pdf";
    PutFile(StorageName, FileName, FileName, True, UUID);
    LoadFileServer(StorageName);
      
EndProcedure


&AtServer
Procedure LoadFileServer(StorageName)

   ObjectValue = FormDataToValue(Object, Type("DocumentObject.Document1"));
   ObjectValue.storage = New ValueStorage(GetFromTempStorage(StorageName));
   ObjectValue.Write();
   ValueToFormData(ObjectValue, Object);
  
   EndProcedure



now how can i open the PDF ?!!! ...sorry for the huge amount of questions today .. but this thing is really difficult :D

Profile
#6
Guest
Points::
Joined::

No problems.

To do this you need to get your file:

Code
&AtServer
Procedure OnCreateAtServer(Cancel, StandardProcessing)
   ObjectValue = FormDataToValue(Object, Type("DocumentObject.Document1"));
   StorageName = PutToTempStorage(ObjectValue.storage.Get(), UUID);
EndProcedure

&AtClient
Procedure ExportFile(Command)
   If Not IsBlankString(StorageName) Then
      GetFile(StorageName, Object.Description, True);
   EndIf;
EndProcedure

Here StorageName is a form attribute with String type.

Profile
#7
Active user
Points:: 0
Joined:: Jun 4, 2013

I added this to the previous code as you said ...as a result a new window is opened and asked me to <open> <save> <cancel> the file ... when i press open nothing is happening


Code

&AtServer
    Procedure OnCreateAtServer(Cancel, StandardProcessing)
        ObjectValue = FormDataToValue(Object, Type("DocumentObject.Document1"));
   StorageName = PutToTempStorage(ObjectValue.storage.Get(), UUID);
    EndProcedure


&AtClient
Procedure pdfview(Command)
   Message(StorageName);
         If Not IsBlankString(StorageName) Then
      GetFile(StorageName,"1C Ebook", True);
   EndIf;

    EndProcedure



sorry again brother for bothring you :)

Profile
#8
Guest
Points::
Joined::

I guess that you are uploading file and right after that trying to see it. As storage name is updated in OnCreateAtServer handler, the file that you have uploaded will not be accessible until you will open your document form again. So you need to update storage name after uploading file:

Code
&AtServer
Procedure LoadFileServer(StorageName)
­
    ObjectValue = FormDataToValue(Object, Type("DocumentObject.Document1"));
    ObjectValue.storage = New ValueStorage(GetFromTempStorage(StorageName));
    ObjectValue.Write();
    ValueToFormData(ObjectValue, Object);

    UpdateStorageName();
  
EndProcedure

&AtServer
Procedure OnCreateAtServer(Cancel, StandardProcessing)
    
    UpdateStorageName();

EndProcedure

&AtServer
Procedure UpdateStorageName()

    ObjectValue = FormDataToValue(Object, Type("DocumentObject.Document1"));
    StorageName = PutToTempStorage(ObjectValue.storage.Get(), UUID);

EndProcedure


And please pay attention to idents in your code as they make code more readable.

Profile
#9
Active user
Points:: 0
Joined:: Jun 4, 2013

This Code worked with me perfectly ... and i just wanna share it whit you guys

Code
&AtClient
Procedure Upload(Command)
    LoadFile();
EndProcedure
         &AtClient
Procedure LoadFile()   
   Object.StorageName = "";
    FileName ="C:\Users\mohammad.maleh\Desktop\hello1c.pdf"; 
   Object.Description=FileName;
   PutFile(Object.StorageName,Object.Description, Object.Description, True, UUID);

   LoadFileServer(Object.StorageName);
            
     EndProcedure


&AtServer
Procedure LoadFileServer(StorageName)
   ObjectValue = FormDataToValue(Object, Type("DocumentObject.Document1"));
   ObjectValue.storage = New ValueStorage(GetFromTempStorage(StorageName));
    ObjectValue.Write();
   ValueToFormData(ObjectValue, Object);

   updateStorageName()

EndProcedure

&AtServer
Procedure OnCreateAtServer(Cancel, StandardProcessing)
   updateStorageName()
EndProcedure


&AtClient
Procedure pdfview(Command)
   
   If Not IsBlankString(Object.StorageName) Then
         GetFile(Object.StorageName,Object.Description, true);
    EndIf;
      

EndProcedure

&AtServer
Procedure UpdateStorageName()
         
   ObjectValue = FormDataToValue(Object, Type("DocumentObject.Document1"));
   Object.StorageName = PutToTempStorage(ObjectValue.storage.Get(), UUID);
   
EndProcedure



where storage is a object attribute with valuestorage type and description and storagename are object attributes with string type.

Profile
#10
Guest
Points::
Joined::

Also please note that if you put file to temp storage when form is created, you make form opening slower, but on the other side your PDF file will be opened faster when a user will execute command. You could also move UpdateStorageName() to PDFView() instead of OnCreateAtServer():

Code
&AtClient
Procedure PDFView(Command)
   
    If Not IsBlankString(Object.StorageName) Then
        UpdateStorageName()
        GetFile(Object.StorageName,Object.Description, true);
    EndIf;
­
EndProcedure

The variant depends on what is more importand. I suppose that in your case this variant would be more efficient.

Profile
#11
Interested
Points:: 0
Joined:: Nov 10, 2011

Dear Mohammad,

I had a very similar task, and a technical specialist of 1C company helped me. I don't know if he likes I mention his name. But I believe he doesn't mind if I share this information with you.

At first we load a file

Code
&AtClient
Procedure LoadFile1(Command)
   StorageName1 = "";
   FileName = Object.FileName;
   If PutFile(StorageName1, FileName, FileName, True, UUID) Then
    LoadFileServer1(StorageName1);
   EndIf;
EndProcedure


Here is "If PutFile(...True) then"  important, because a user can just close this form and don't attach a file.

Then we save it into the Storage. In my case it is an attribute Storage1 (I have 4 of them)

Code
&AtServer
Procedure LoadFileServer1(StorageName1)
   ObjectValue = FormDataToValue(Object, Type("DocumentObject.Project"));
   ObjectValue.Storage1 = New ValueStorage(GetFromTempStorage(StorageName1));
   ObjectValue.Write();
   ValueToFormData(ObjectValue, Object);



And then

Code
&AtClient
Procedure OpenFile1(Command)
   
   TempFileName = GetTempFileName("pdf"); // I need only pdf-files
   StorageURL = GetURL(Object.Ref, "Storage1");
   GetFile(StorageURL, TempFileName, False);   
   RunApp(TempFileName);

EndProcedure


StorageURL is important, because we can not directly access Object.Storage1.    GetFile(...) saves the file onto the disc and RunApp(...) is a function to open this file.

And I do not use

Code
//ObjectValue = FormDataToValue(Object, Type("DocumentObject.Project"));
    //StorageName = PutToTempStorage(ObjectValue.Storage.Get(), UUID);


I hope it helps. Good luck!

Profile
#12
Active user
Points:: 0
Joined:: Jun 4, 2013

Thanks for Sharing this Lioudmila, it's very helpful for me and for the other people :)

Profile
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)



© 1C LLC. All rights reserved
1C Company respects the privacy of our customers and visitors
to our Web-site.