Code Monkey home page Code Monkey logo

Comments (8)

hichamhargal avatar hichamhargal commented on May 29, 2024 4

that's because of the Retina scaling feature of Fabric.js. canvas is scaled by devicePixelRatio (it's 4 in my S9 for example) instead of 1 in the desktop. see here.

so, concerning your issue, you have just to disable "enableRetinaScaling" attribute in the creation of the fabrice object.

in pdfannotate.js, change the function initFabric:

this.initFabric = function () {
		var inst = this;
	    $('#' + inst.container_id + ' canvas').each(function (index, el) {
	        var background = el.toDataURL("image/png");
	        var fabricObj = new fabric.Canvas(el.id, {
	            freeDrawingBrush: {
	                width: 1,
	                color: inst.color
	            },
		enableRetinaScaling: false // <====== add this line
	        });
			inst.fabricObjects.push(fabricObj);
			if (typeof options.onPageUpdated == 'function') {
				fabricObj.on('object:added', function() {
					var oldValue = Object.assign({}, inst.fabricObjectsData[index]);
					inst.fabricObjectsData[index] = fabricObj.toJSON()
					options.onPageUpdated(index + 1, oldValue, inst.fabricObjectsData[index]) 
				})
			}
	        fabricObj.setBackgroundImage(background, fabricObj.renderAll.bind(fabricObj));
	        $(fabricObj.upperCanvasEl).click(function (event) {
	            inst.active_canvas = index;
	            inst.fabricClickHandler(event, fabricObj);
			});
			fabricObj.on('after:render', function () {
				inst.fabricObjectsData[index] = fabricObj.toJSON()
				fabricObj.off('after:render')
			})
		});
	}

hope that helps.

from pdfjsannotations.

RavishaHesh avatar RavishaHesh commented on May 29, 2024

You export and save the JSON

pdf.serializePdf(); // returns JSON string with canvas data

pdf.loadFromJSON(serializedJSON) // continue edit with saved JSON

from pdfjsannotations.

crackedurheart007 avatar crackedurheart007 commented on May 29, 2024

Sorry sir, I'm a student.
How to convert JSON to pdf file?

from pdfjsannotations.

RavishaHesh avatar RavishaHesh commented on May 29, 2024

When you open PDF file from this plugin, it converts PDF pages into images and render those images into FabricJS canvases. that's how you were able to draw things on top of the PDF. When you serialize those canvases it will convert those images to base64 format and outputs a JSON with all annotations. So when you want to open that file again you can pass that JSON into pdf.loadFromJSON function. The biggest drawback of this plugin is this process removes the ability to select text from PDF. I have started to fix this issue but it's still early dev stage.

from pdfjsannotations.

crackedurheart007 avatar crackedurheart007 commented on May 29, 2024

Thank you so much for detail information and guideline.

from pdfjsannotations.

hichamhargal avatar hichamhargal commented on May 29, 2024

I think that he want to upload the final pdf as a file on the server.
if it's what you want, you can do it with ajax after rendering with doc.output() instead of doc.save().

to do so, in script.js add the function:

function saveToServer(link) {
    pdf.saveToServer(link);
}

then, add in pdfannotate.js the function:

PDFAnnotate.prototype.saveToServer = function (link) {
	var inst = this;
	var doc = new jsPDF();
	$.each(inst.fabricObjects, function (index, fabricObj) {
	    if (index != 0) {
	        doc.addPage();
	        doc.setPage(index + 1);
	    }
	    doc.addImage(fabricObj.toDataURL(), 'png', 0, 0);
	});

        var file = new File([doc.output('blob')], 'file.pdf', { type: "application/pdf"});
	var formData = new FormData();
	formData.append('file', file);
	
	jQuery.ajax(link,
	{
		method: 'POST',
		data: formData,
		processData: false,
		contentType: false,
		success: function(data){console.log(data)},
		error: function(data){console.log(data)}
	});
}

finally, change the button in index.html to point to the new function:

<button class="btn btn-light btn-sm" onclick="savePDF()"><i class="fa fa-save"></i> Save</button>

to

<button class="btn btn-light btn-sm" onclick="saveToServer('link_to_backend_function')"><i class="fa fa-save"></i> Save</button>

the link_to_backend_function point to the method receiving and storing the pdf in the server with your back-end language.

hope that helps.

from pdfjsannotations.

crackedurheart007 avatar crackedurheart007 commented on May 29, 2024

@hichamhargal Thank you so much. It's working. But when I access from tablet (iPad), it crop my document and big file size. Do you have solution?

from pdfjsannotations.

RavishaHesh avatar RavishaHesh commented on May 29, 2024

@crackedurheart007 hope above answer fixed your issue. I'm gonna close this for now. Feel free to re-open if you need

from pdfjsannotations.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.