Align camera to view precisely and automatically, without manual fiddling
I created this script based on operator simple template. 

import bpy

def main(context):
    if context.area.type == 'VIEW_3D':

        #set render resolution and lens data
        region=False;
        for reg in context.area.regions:
            if(reg.type=='WINDOW'): region=reg;

        if(region):
            context.scene.render.resolution_x=region.width
            context.scene.render.resolution_y=region.height
            context.scene.camera.data.lens=context.space_data.lens

        #set camera position
        context.scene.camera.matrix_world=context.space_data.region_3d.view_matrix
        context.scene.camera.matrix_world.invert()

        #I don't know why sensor width must be 64, but it works:
        context.scene.camera.data.sensor_width=64

        #set view from camera
        context.space_data.region_3d.view_perspective = 'CAMERA'


class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.set_render_size"
    bl_label = "set render size"


    def execute(self, context):
        main(context)
        return {'FINISHED'}


def register():
    bpy.utils.register_class(SimpleOperator)


def unregister():
    bpy.utils.unregister_class(SimpleOperator)


if __name__ == "__main__":
    register()


You can launch this script, then use set render size (you can set outer name if you want) operator.



Or you can add additional information to create an addon from this.