I have a api and this api returning a binary pdf.
This pdf is changing everytime when a request come and i don't want the store it.
Simply i want the create a button and this should trigger a function.
Function should be take my api response (binary pdf) and show this pdf to user in interface. (with download button)
Thanks for any suggestion
I would write a controller to return the file, and put a button on the view or in the header with action to call the controller.
from odoo.http import request, content_disposition, Controller
class MyController(Controller):
#http.route('/download_file', auth='user', type='http')
def download_file(self, id, **kw):
# call the external API to retrieve the file
file_content = requests.get('http://example.com/api/file').content
# set the filename and content type
filename = 'my_file.txt'
content_type = 'text/plain'
# send the file as a response to user and prompt them to download it
return request.make_response(file_content,
[('Content-Type', content_type),
('Content-Disposition', content_disposition(filename))])
Related
I created a document file from word and has exported as pdf . i want to show the pdf content inside the Div element in razor page. How can I show the pdf content from razor page. Please can you provide an example code how to show in blazor server side
If you stored your pdf file directly in your documents for example in the folder wwwroot/pdf.
wwwroot/pdf/test.pdf
You can display this PDF with this line of html bellow :
< embed src="pdf/test.pdf" style="width=100%; height=2100px;" />
It will provide you a pdf displayer with printing options !
If you want to go further, upload your file and then display it, I will recommend you to go check this explanation :
https://www.learmoreseekmore.com/2020/10/blazor-webassembly-fileupload.html
The upload for PDF files works the same as Img file, you need to go check IBrowserFile documentation.
You will see that it has a Size obj and a OpenReadStream() function that will help you get the display Url for your file (image or pdf)
If the site abow closes, this is the upload code that is shown on it :
#code{
List<string> imgUrls = new List<string>();
private async Task OnFileSelection(InputFileChangeEventArgs e)
{
foreach (IBrowserFile imgFile in e.GetMultipleFiles(5))
{
var buffers = new byte[imgFile.Size];
await imgFile.OpenReadStream().ReadAsync(buffers);
string imageType = imgFile.ContentType;
string imgUrl = $"data:{imageType};base64,{Convert.ToBase64String(buffers)}";
imgUrls.Add(imgUrl);
}
}
}
This code was written by Naveen Bommidi, author of the blog where I found this usefull code
If you want, as I said, upload a PDF and then display it.
You can use the same html line :
< embed src="#imgUrl" style="width=100%; height=2100px;" />
And your uploaded files will be displaying.
Example
I stored image file in path djangor_prject_name/pictures/image_name.jpg by using
models.ImageField(upload_to='pictures/services/', max_length=255, null=True, blank=True)
and file is uploaded successfully.
But while retrieving, what is the exact url to view image?
Because id Database, the value is just
picutes/services/image_name.jpg
So, in my view file, I am using vue.js and need full path or url of the image to display.
After creation it won't give you full url, but in the list it will come with full url. So for that you use custom serializers.SerializerMethodField.
class TestSerializer(serializers.ModelSerializer):
photo = serializers.SerializerMethodField()
class Meta:
model = Test
fields = ('photo',)
def get_photo(self, car):
request = self.context.get('request')
photo = Test.photo.url
return request.build_absolute_uri(photo)
For further details refer here
I have a webmail and i want the user to press on a picture in the webmail and then be linked back to my mvc project view. I don't have a real domain yet so i can't type www.mysite.com. I want to be able to link to the view without depending on which domain the site is currently running on.
in my webmail i have the body that looks like this:
string Body = "<b>Välj ett alternativ!</b><br><br><a href='cid:path4'/><img src='cid:Happy'/ alt='HTML tutorial' width='120' height='120'></a><input type='image' src='cid:Orange'/ name='image' width='120' height='120'><input type='image' src='cid:Mad'/ name='image' width='120' height='120'>";
where the cid:path4 is i want the url och what ever so that i linkes me back to my project.
You'll have to format the absolute path into the body text from your controller action (or pass it through to whatever method generates the body):
var path = Request.Uri.AbsoluteUri;
var emailBody = String.Format("Your email body. Link", path);
Or, you could link to another controller action with Url.Action:
var path = Url.Action("MyAction", "MyController", null, "http");
var emailBody = String.Format("Your email body. Link", path);
Of course, this all assumes that this email is sent by a controller action - if it's not, it may not be possible to determine the absolute URI of the website.
When I fill and submit pdf form, send_from_directory returns the old file. I use pdftk to push fdf to pdf and flatten, but if I give the same filename as output with the old one, Flask shows the old file. Why isn't it overwritten by the new one? Thanks.
#app.route('/uploads/<filename>')
def show_pdf_form(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
#app.route('/get_data_as_fdf', methods = ['GET', 'POST'])
def get_pdf_data_as_fdf():
# create fdf file
fdf_string = request.data
fdf_file = open(os.path.join(app.config['UPLOAD_FOLDER'], "data.fdf"), "w")
fdf_file.write(fdf_string)
fdf_file.close()
# merge fdf to pdf and flatten pdf
call(["pdftk", "./uploads/send_data_as_fdf.pdf", "fill_form",
"./uploads/data.fdf", "output", "output.pdf", "flatten"])
return redirect(url_for('show_pdf_form', filename = filename))
These are related code snippets. Thank you.
Flask send_from_directory uses send_file internally. send_file has cache timeout which you can set to 0.
Try clearing the cache and hitting the URL in incognito mode. if it works, you can do:
send_from_directory(app.config['UPLOAD_FOLDER'], filename, cache_timeout=0)
Or you can try any of the options mentioned in:
Disabling caching in Flask
I'm working to refactor a PDF form web application that is using the Active PDF Toolkit and the FDFToolkit from Adobe. My goal is to use iTextSharp to:
Pre-populate the form fields with data from the database
Allow the user to attach a signature and/or barcode image via FDF
Item #1 is not the problem. Item #2 is the biggest challenge. Let me provide some background:
This is a web application which renders the PDF form once. After the initial load, there are 2 key buttons on the form which submit the PDF form to a URL with an action parameter in the query string. These buttons are called "Save" and "Sign". The Save button takes the FDF field dictionary and saves it to the database. The Sign button looks up the signature for the logged-in user and attaches the signature image to the FDF and writes the FDF to the HTTP Response.
The FDFToolkit supports attaching an image to a field using this method:
FDFSetAP(string bstrFieldName, short whichFace, string bstrFileName, short pageNum)
iTextSharp does not offer a comparable method in the FdfWriter class. I've considered subclassing the FdfWriter class and adding my own method to attach an image, but wanted to reach out here to see if anyone has had the same problem.
I have been able to overlay an image on top of a field using this method, but this is in the underlying PDF and not the FDF.
AcroFields.FieldPosition pos = _Stamper.AcroFields.GetFieldPositions("SIGNATUREFIELD").First();
Image signature = Image.GetInstance("Signature.gif");
image.SetAbsolutePosition(pos.position.Left, pos.position.Bottom);
image.ScaleToFit(pos.position.Width, pos.position.Height);
PdfContentByte pcb = _Stamper.GetOverContent(pos.page);
pcb.AddImage(image);
Thanks!
I've put images on forms by using the PdfStamper and making Pushbutton fields. You can replace your existing field with a Pushbutton field and set the Pushbutton to READ_ONLY so that it can't be pressed and it will look like a static image. This will keep the image you're trying to add as a field annotation instead of adding it to the page content.
using (PdfStamper stamper = new PdfStamper(new PdfReader(inputFile), File.Create(outputFile)))
{
AcroFields.FieldPosition fieldPosition = stamper.AcroFields.GetFieldPositions(fieldName)[0];
PushbuttonField imageField = new PushbuttonField(stamper.Writer, fieldPosition.position, fieldName);
imageField.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
imageField.Image = iTextSharp.text.Image.GetInstance(imageFile);
imageField.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
imageField.ProportionalIcon = false;
imageField.Options = BaseField.READ_ONLY;
stamper.AcroFields.RemoveField(fieldName);
stamper.AddAnnotation(imageField.Field, fieldPosition.page);
}